2015-11-06 38 views
0

这就是我如何运作的,但我不喜欢它的外观。我真想让我写它像这样使用Underscore时无法在侦听器上获得videojs _.bind

displayVideo: function displayVideo() { 
    this.videoJs = videojs('player', {}, _.bind(function() { 
    this.on('play', this.onPlayerPlayButton); 
    this.on('pause', this.onPlayerPauseButton); 
    }, this)); 
}, 

回答

1

这是下划线库中的样品其工作中使用_.bind

displayVideo: function displayVideo() { 
    let _this = this; 
    this.videoJs = videojs('player', {}, function() { 
    this.on('play', _this.onPlayerPlayButton); 
    this.on('pause', _this.onPlayerPauseButton); 
    }); 
}, 

的。对事件侦听器不能正常工作。

var func = function(greeting){ return greeting + ': ' + this.name }; 
func = _.bind(func, {name: 'moe'}, 'hi'); 
func(); 

此示例正在工作。该函数正在绑定到此对象(您的对象的displayVideo方法的调用方)

请尝试从下划线的页面中的下划线http://underscorejs.org/的页面中尝试此示例,以便了解它是如何工作的。

它会调用该函数FUNC说这个名字在那里这是你_.bind下划线函数的第二个参数,对象的“嗨” ARGS {名称:“萌”}

+0

我不能给你一个使用您共享的代码进行工作示例,因为很难看出您的方法displayVideo的调用者是谁,所以我无法确定范围是否适合您的案例。 –

+0

没关系,我让它像这样工作让vid = this; vid.on('play',_.bind(self。onPlayerPlayButton,self));在承诺的内部,我已经让自己=这个外面 – devwannabe

相关问题