我试图用两个对象创建一个函数调用链。Javascript中的嵌套函数参数和'this'上下文
我已经添加代码中的注释来描述我想要做的事:
function Huh(parentContext) {
this.parentContext = parentContext;
this.check = function() {
console.log(parentContext);
}
this.DoWork = function(successFunc) {
console.log('Huh.DoWork');
successFunc('yay');
};}
function Thing() {
this.nextSuccess = function(e) {
console.log('nextSuccess ' + e);
};
this.success = function(e) {
console.log('success!! ' + e);
var h = new Huh(this); // It looks like 'this' doesn't mean the Thing context any more. ?!?!
//h.check();
h.DoWork(this.nextSuccess); // THIS BREAKS.
};
this.fail = function() {
console.log('fail');
};
this.firstBit = function(successFunc, failFunc) {
var h = new Huh(this);
//h.check();
h.DoWork(this.success);
};
// start with this function
this.Go = function() {
this.firstBit(this.success, this.fail);
};}
这一切都休息,当我尝试在Thing.success创建咦的第二个实例。
我尝试通过this.nextSuccess,但它看起来像'这个'上下文是不一样的。
请帮忙。
当您使用嵌套函数时,请尝试在缩进中反映这一点。它使阅读起来更容易。 – Skilldrick 2010-06-29 09:46:59
对不起。我试图让缩进工作,但代码块一直在打破。 – 2010-06-29 09:57:43