gpt4 book ai didi

javascript - 将参数传递到主干 .bind() 函数

转载 作者:行者123 更新时间:2023-12-01 03:07:01 24 4
gpt4 key购买 nike

我有一个呈现视频播放器和视频列表的 View 。

initialize: function() {
var q = window.location.search.slice(3);
this.videos = new Videos(window.exampleVideoData);
this.videos.bind('select', this.notifyAppView, this);
if ( q ){
this.videos.bind('fetchFinished', function(){
console.log('should still work');
this.render();
}, this);
// Any time this.videos collection changes we will re-render
this.videoPlaying = this.videos.models[0];
this.videos.fetch( q );
}else{
//Collection of all videos

// Any time this.videos collection changes we will re-render
this.videoPlaying = this.videos.models[0];
this.render();
}

},

这是我在 View 上的初始化函数。我遇到的问题是,当我将参数传递到绑定(bind)函数时,出现错误

app.js:10 Uncaught SyntaxError: Unexpected string

就是这一行:

this.videos.bind('fetchFinished', function(){
console.log('should still work');
this.render();

},这个);

当我们将字符串传递给函数时,我们会收到错误,如下所示:

this.videos.bind('fetchFinished', function('adfadf'){
console.log('should still work');
this.render();
}, this);

指定要与主干绑定(bind)并带有参数的函数的正确方法是什么?

最佳答案

这是不正确的语法。您应该在函数声明中指定参数的名称,而不是值。

您可以传入这样的值:

this.videos.bind('fetchFinished', function(arg){
console.log('should still work', arg); // should still work adfadf
this.render();
}.bind(this, 'adfadf'));

请注意,第二个 bind 是函数对象的 native bind 方法,而不是主干 bind。最好使用backbone的on而不是使用它的别名bind以避免混淆

关于javascript - 将参数传递到主干 .bind() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46107507/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com