[英]javascript chainning return this from callback
我试图从回调中获得返回值,但是我总是无法定义。
这是被抢断的
create: function(currentView, data){
var itsMe = this;
this.thumbsWrapper = this.templates.wrapper().hide();
currentView.append(this.thumbsWrapper);
this.thumbsWrapper.fadeIn("fast", function(){
return itsMe;
});
},
var l = list().create(currentView); //need teh return that i can use chaining
var l现在是未定义的,如果我将fadeIn与回调一起使用...如果我不将fadeIn与回调一起使用,它将返回obj
有人知道为什么吗?
@Felix Kling说的是正确的,您什么也不返回。 如果要返回itsMe
,则需要执行以下操作:
create: function(currentView, data){
var itsMe = this;
this.thumbsWrapper = this.templates.wrapper().hide();
currentView.append(this.thumbsWrapper);
this.thumbsWrapper.fadeIn("fast");
return itsMe;
}
如果要链接,哪个就足够了。
如果要在淡出完成时获得对itsMe
的引用,则需要传递自己的回调:
create: function(currentView, data, callback){
var itsMe = this;
this.thumbsWrapper = this.templates.wrapper().hide();
currentView.append(this.thumbsWrapper);
this.thumbsWrapper.fadeIn("fast", function(){
callback(itsMe);
});
}
list().create(function (that) {
console.log("fade out complete");
console.log("itsMe is", that);
});
如果你想有一个链接模式,当淡出完成后,将在链执行下一个功能,您将需要而不是引用传递给this
可是一个对象,它可以排队的命令,实现每个指令顺序。
您需要在create()
函数中返回该对象,该对象当前不返回任何内容:
create: function(currentView, data){
var itsMe = this;
this.thumbsWrapper = this.templates.wrapper().hide();
currentView.append(this.thumbsWrapper);
this.thumbsWrapper.fadeIn("fast", function(){
return itsMe; //<--- this isn't going anywhere because you don't capture it
});
return itsMe; //<------ return the object
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.