I wrote a proxy of HTMLAudioElement. When I call play() method via the proxy.
const proxy = new Proxy( audioPlayer, {
get(target: typeof audioPlayer , prop, receiver) {
return Reflect.get(target, prop, receiver);
}
});
proxy.play();
I got a Error:
TypeError: Failed to execute 'play' on 'HTMLMediaElement': Illegal invocation
As far as I know, it is because that the play function is not bind to target.
However, Shouldn't the prop receiver do the binding for us? Or something wrong in my code?
Then, I have to change my code as below, it looks weired, but works.
const proxy = new Proxy( audioPlayer, {
get(target: typeof audioPlayer , prop, receiver) {
if(prop=='play'){
return target.play.bind(target)
}else if(prop=='pause'){
return target.pause.bind(target)
}
return Reflect.get(target, prop, receiver);
}
});
proxy.play();