admin管理员组文章数量:1356876
I have the following code:
var myPage = {};
myPageponent = function(callback){
var somethingHappened = true;
if (somethingHappened){
callback();
}
};
myPage.main = function(){
// Initialise.
this.init = function(){
// make an instance of my ponent
thisponent = new myPageponent( this.callback );
// need my utility function here
this.doSomethingUseful();
};
// Callback to be executed when something happs in the ponent.
this.callback = function(){
this.doSomethingUseful(); // doesn't work
};
// A useful utility that needs to be accessible from both the
// init() and callback() functions
this.doSomethingUseful = function(){
// some utility stuff
};
};
new myPage.main().init();
What's the best way for me to ensure that the myPage.main scope is available when the callback function is executed from the ponent?
I have the following code:
var myPage = {};
myPage.ponent = function(callback){
var somethingHappened = true;
if (somethingHappened){
callback();
}
};
myPage.main = function(){
// Initialise.
this.init = function(){
// make an instance of my ponent
this.ponent = new myPage.ponent( this.callback );
// need my utility function here
this.doSomethingUseful();
};
// Callback to be executed when something happs in the ponent.
this.callback = function(){
this.doSomethingUseful(); // doesn't work
};
// A useful utility that needs to be accessible from both the
// init() and callback() functions
this.doSomethingUseful = function(){
// some utility stuff
};
};
new myPage.main().init();
What's the best way for me to ensure that the myPage.main scope is available when the callback function is executed from the ponent?
Share Improve this question edited Oct 30, 2012 at 11:49 Xoundboy asked Oct 29, 2012 at 18:23 XoundboyXoundboy 8361 gold badge16 silver badges26 bronze badges 2-
The
this
keyword has nothing to do with the variable scope of the function – Bergi Commented Oct 29, 2012 at 18:30 - better to correct someone rather than simply stating that they are incorrect. @Xoundboy you're thinking about context, not scope. – Ilia Choly Commented Nov 1, 2012 at 17:49
3 Answers
Reset to default 7use bind:
this.callback = function(){
this.doSomethingUseful(); // doesn't work
}.bind(this);
If you want to supply scope, you can use Function.prototype.call
.
var foo = 'bar';
function(){
// this = 'bar';
}.call(foo);
Instantiate object with this function:
function newClass(klass) {
var obj = new klass;
$.map(obj, function(value, key) {
if (typeof value == "function") {
obj[key] = value.bind(obj);
}
});
return obj;
}
This will do automatic binding of all function, so you will get object in habitual OOP style, when methods inside objects has context of its object.
So you instantiate you objects not through the:
var obj = new myPage();
But:
var obj = newClass(myPage);
本文标签: Javascriptscope is lost in callbackStack Overflow
版权声明:本文标题:Javascript - scope is lost in callback - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743985429a2571256.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论