[英]javascript chainning return this from callback
i've tried to get a return this from a callback, but i always get undefined. 我试图从回调中获得返回值,但是我总是无法定义。
here is the snipped 这是被抢断的
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
the var l is now undefined, if i use the fadeIn with a callback... if i dont use fadeIn with the callback it returns the obj var l现在是未定义的,如果我将fadeIn与回调一起使用...如果我不将fadeIn与回调一起使用,它将返回obj
anyone an idea why? 有人知道为什么吗?
What @Felix Kling say is correct, you are not returning anything. @Felix Kling说的是正确的,您什么也不返回。 If you want to return
itsMe
you will need to do: 如果要返回
itsMe
,则需要执行以下操作:
create: function(currentView, data){
var itsMe = this;
this.thumbsWrapper = this.templates.wrapper().hide();
currentView.append(this.thumbsWrapper);
this.thumbsWrapper.fadeIn("fast");
return itsMe;
}
Which should suffice if you want chaining. 如果要链接,哪个就足够了。
If you want to get a reference to itsMe
when the fadeout is finished, you will need to pass your own callback: 如果要在淡出完成时获得对
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);
});
And if you want to have a chaining pattern, which will execute the next function in the chain when fadeout is finished, you will need to pass not a reference to this
but an object which can queue up commands, implement each command sequentially. 如果你想有一个链接模式,当淡出完成后,将在链执行下一个功能,您将需要而不是引用传递给
this
可是一个对象,它可以排队的命令,实现每个指令顺序。
You need to return the object in the create()
function, it is currently not returning anything: 您需要在
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.