javascript anonymous function parameter passing

后端 未结 5 531
终归单人心
终归单人心 2021-02-02 13:04

I have some javascript code (within an object) :

toggle: function() {
    var me = this;
    var handler = function() { me.progress() };
    me.intervalId = setI         


        
5条回答
  •  情歌与酒
    2021-02-02 13:20

    You can use ".bind()":

    var handler = function() { this.progress(); }.bind(this);
    

    New browsers have "bind()", and the Mozilla docs have a solid implementation you can use to patch older browsers.

提交回复
热议问题