Javascript 更改“;var";在创建新实例的构造函数闭包内
我的英语写得很差,很抱歉 我对Javascript 更改“;var";在创建新实例的构造函数闭包内,javascript,jquery,closures,this,Javascript,Jquery,Closures,This,我的英语写得很差,很抱歉 我对var在函数闭包中的行为有一些疑问,如: var c = function(){ var _self = this; } 当我装入新实例时: var A = new c(); A.logSelf = function(){ console.log(_self); } 在一个作用域(或闭包…我不知道正确的名称)中,\u self变量是未定义的,但是,我可以添加一个init()方法来重新评估所有新实例中的\u self,并使用jQuery帮助扩展构
var
在函数闭包中的行为有一些疑问,如:
var c = function(){
var _self = this;
}
当我装入新实例时:
var A = new c();
A.logSelf = function(){
console.log(_self);
}
在一个作用域(或闭包…我不知道正确的名称)中,\u self
变量是未定义的
,但是,我可以添加一个init()
方法来重新评估所有新实例中的\u self
,并使用jQuery帮助扩展构造函数的默认行为(简单来说):
现在,如果我在控制台日志A中调用A.sayName()
&A.sayA()
,我会说:“哇,我是有史以来最好的!”,但当我尝试实例AB
var时,一切都变了:
var B = new c({
name : "B",
init = function(){
_self = this;
}
});
。。。现在A.sayName()
logsA,但是A.sayA()
logsB(因为\u self
在c的所有实例中现在是我创建的最后一个实例[B在本例中])
为什么?我怎么了
也许A和B的闭包是相同的,因为它们都是同一个构造函数的实例?
但是为什么在c方法中使用A和B来确定\u self
的值是正确的?我不明白这有什么意义!唉
工作示例
为什么我需要这个?
在我的A&B实例中,我使用了一些$.ajax和jQuery处理程序,在jQuery用来回调(大家都知道)的匿名函数中,这个
是对某些东西的引用,我不能引用包装匿名函数的实例
我需要\u self
以便在每次需要使用jQuery回调或使用$.ajax的上下文属性时,都可以轻松引用而不必重新评估var\u self
我更喜欢一次评估self
,在我实例的范围(或闭包)内,它不会在那里更改他对这个
的引用
举个例子。。。
真正的问题在于A方法的内部:
endpoint : function(){
//var __self = this;
this.$el.find(".call-to").on("click", function(e) {
e.preventDefault();
if(typeof __static.B !== "undefined") return __static.B.show();
//__static is the reference of the scope (all works inside a self invoked anonymous function).
__static.B = new __static.C(Bconfig, __static); //Bconfig is an object that contain all extend and override the C constructor.
__self.close();
__static.B.view = new _viewConf(__static.B.config.type[__self.view.b]);
__static.B.render();
});
},
在这两种情况下,每次需要使用时,我都需要对self进行竞争
(我在这里搜索了一个答案,并且在google中搜索了一个答案……但我没有找到我的具体案例)这里的问题是,在实例A(和B)中分配给的自变量与函数c范围中的自变量不同,它是一个全局变量(在窗口对象中) })) 这样,对象A和对象B都在其函数中使用window.\u self,因此使它们共享数据。这里要学习的是,您不能使用“var”声明操作函数范围中定义的变量。您可以操作的那些必须分配给“this”(对于构造函数) 此代码段有一个函数B.init,它将全局
\u self
设置为B(This)
当您定义一个类似的代码时,它也在编辑全局\u self
也许你想要的是这样的东西
[]
使用$.ajax
的上下文
功能没有什么错。但你可以有这样的东西:
var c = function(obj){
var _self = this,
data;
...
this.request = function(){
$.ajax( {
url : 'blah',
// context : this,
success : function(d){
data = d; // setting a private var of A
_self.sayName(); // in lieu of using context to set "this". calling A public method
}
}); // end ajax;
} // end anon;
} // end constructor;
没有问题如果方法在
c
构造函数内部,那么问题就在方法内部,该方法在c
的新实例中重写或扩展构造函数……在这种情况下,既然您正在扩展新对象,因此有一个对已创建对象的引用,为什么不使用该引用?你不必在实例化某个东西的那一刻就扩展它。我不确定你是否理解了。。。但这是对的<代码>\u self在A&B实例中取值是窗口的一个属性。这就是我问题的答案。您建议直接使用B.method()
和A.method()
?好的。。。清楚的所以,我可以做我想做的事?(我糟糕的英语!)如果可能的话,可以发布$.ajax()
回调片段吗?我用一些例子更新我的问题:)
endpoint : function(){
//var __self = this;
this.$el.find(".call-to").on("click", function(e) {
e.preventDefault();
if(typeof __static.B !== "undefined") return __static.B.show();
//__static is the reference of the scope (all works inside a self invoked anonymous function).
__static.B = new __static.C(Bconfig, __static); //Bconfig is an object that contain all extend and override the C constructor.
__self.close();
__static.B.view = new _viewConf(__static.B.config.type[__self.view.b]);
__static.B.render();
});
},
var c = function(obj){
var _self = this; // scope of function 'c'
this.name = "lol";
this.init = function(){
_self = this; // still scope of function 'c'
}
this.sayName = function(){
console.log(_self.name); // scope of function 'c'
}
$.extend(this,obj);
this.init();
}
var A = new c({
name : "A",
init : function(){
_self = this; // global scope here, _self === window._self
},
sayA : function(){
console.log(_self.name) // also call to the global _self object
}
var B = new c({
name : "B",
init = function(){
_self = this;
}
});
var c = function(obj){
var _self = this,
data;
...
this.request = function(){
$.ajax( {
url : 'blah',
// context : this,
success : function(d){
data = d; // setting a private var of A
_self.sayName(); // in lieu of using context to set "this". calling A public method
}
}); // end ajax;
} // end anon;
} // end constructor;