html - How can I override a method in Javascript and apply it to the original object, not a child? - Stack Overflow

I'm trying to override the play() method of the Audio object, but I don't want to do so just

I'm trying to override the play() method of the Audio object, but I don't want to do so just for a child object. I want to apply it to the standard Audio object.

The problem is I also want to use the original play() method. I've tried cloning the Audio object, making the changes, while calling the original Audio object's play() method and re-assigning back to the original Audio object. It doesn't work.

Ideas anyone? As an example, how could you add an alert() in the play() method while still calling the original play method?

plz. I need to do this because I have calling code generated by a tool that is calling new Audio. So it will be a super pain to constantly have to do a replace-all on this for the hundreds of code generations I'm doing.

I'm trying to override the play() method of the Audio object, but I don't want to do so just for a child object. I want to apply it to the standard Audio object.

The problem is I also want to use the original play() method. I've tried cloning the Audio object, making the changes, while calling the original Audio object's play() method and re-assigning back to the original Audio object. It doesn't work.

Ideas anyone? As an example, how could you add an alert() in the play() method while still calling the original play method?

plz. I need to do this because I have calling code generated by a tool that is calling new Audio. So it will be a super pain to constantly have to do a replace-all on this for the hundreds of code generations I'm doing.

Share Improve this question edited Jun 12, 2012 at 9:12 Talha 19.3k8 gold badges52 silver badges66 bronze badges asked Jun 12, 2012 at 7:10 faceyspacey.faceyspacey. 2,6302 gold badges22 silver badges31 bronze badges 1
  • 1 Can we see the code you tried? :-) – Florian Margaine Commented Jun 12, 2012 at 7:15
Add a ment  | 

3 Answers 3

Reset to default 5
Audio.prototype.play = ( function( old ) {
   var a=1, b=2, c=3; // your 'private' variables - if needed;
   return function() {
      console.log( a+b+c );// do something   
      return old.apply( this, arguments );// return 'original' results
   }
} )( Audio.prototype.play );

Basically this could be done like this:

var playOriginal = Audio.prototype.play;
Audio.prototype.play = function(){
    playOriginal.apply(this, arguments);
}

But I'm not sure this will work for all browsers. Extending of native JS object is not remended in most cases.

You can save the original play method, then replace the play method with a new method that can call the original method whenever it wants to.

Audio.prototype.playOriginal = Audio.prototype.play;
Audio.prototype.play = function(){
    this.playOriginal.apply(this, arguments);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744709110a4589233.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信