这个问题已经被提出并且提出的解决方案是使用'bind'。但是如何在这种情况下使用“绑定”?在原型中保留'this'上下文
var Fun = function(){
this.count = 100;
}
Fun.prototype.f = function(){
console.log("in f : " + this.count);
}
Fun.prototype.g = {
f : function(){
console.log("in g-f : " + this.count);
// Is it possible to use 'bind' here to access 'this' of 'Fun'
}
}
fun = new Fun();
fun.f(); // Results - in f : 100
fun.g.f(); // Results - in g-f : undefined
fun.g.f.bind(fun)(); // Results - in f : 100
是否有可能使用bind
在这样g.f
fun.g.f()
会给结果in f : 100
?
@NEB:这是不可能的,那些是两个不同的对象。 – zerkms