admin管理员组文章数量:1279207
I'm defining a 'class' in JavaScript by means of prototype.
The first time func() runs, it works, but when it's called the second time, through a setTimeout, it fails because this time it has lost the object context, I.E. this doesn't reference the object anymore but now references window.
Is there a way I can overe this while still using prototype? or do I need instead to use closures to define a 'class'?
function klass(){}
klass.prototype = {
a: function() {
console.log( "Hi" );
},
func: function(){
this.a();
setTimeout( this.func, 100 );
}
};
var x = new klass();
x.func();
I'm defining a 'class' in JavaScript by means of prototype.
The first time func() runs, it works, but when it's called the second time, through a setTimeout, it fails because this time it has lost the object context, I.E. this doesn't reference the object anymore but now references window.
Is there a way I can overe this while still using prototype? or do I need instead to use closures to define a 'class'?
function klass(){}
klass.prototype = {
a: function() {
console.log( "Hi" );
},
func: function(){
this.a();
setTimeout( this.func, 100 );
}
};
var x = new klass();
x.func();
Share
Improve this question
edited Sep 13, 2017 at 17:02
user663031
asked Dec 22, 2011 at 22:14
PetruzaPetruza
12.3k26 gold badges87 silver badges143 bronze badges
2
-
3
Just fyi functions don't have any context on their own, it is resolved every time the function is called and only for that call. So it doesn't matter where and how the function is defined, it only matters how the function is called at a particular time.
x.func()
the function is called as a property ofx
.var y = {func: x.func}; y.func()
the function is called as a property ofy
and thethis
isy
for that call. See where I am going with this? setTimeout always calls the function with window set tothis
. – Esailija Commented Dec 22, 2011 at 22:23 - possible duplicate of JavaScript Callback Scope – Felix Kling Commented Dec 22, 2011 at 22:29
3 Answers
Reset to default 8Use Function.prototype.bind
:
setTimeout( this.func.bind(this), 100 );
From Mozilla Developer Network:
https://developer.mozilla/en/JavaScript/Reference/Global_Objects/Function/bind
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5 internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {},
fBound = function () {
return fToBind.apply(this instanceof fNOP
? this
: oThis || window,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
You can wrap it in a function:
var self = this; // reference the value of "this"
setTimeout( function() {self.func();}, 100 );
Use Function.prototype.bind
, or wrap your setTimeout
callback in its own closure:
func: function() {
var self = this;
self.a();
setTimeout( function() {
self.func();
}, 100);
}
版权声明:本文标题:javascript - Passing a prototype's function as parameter without losing the 'this' context - Stack Overf 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741241062a2363987.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论