[英]How private function created with closure can access variable defined in constructor?
I have the following code : 我有以下代码:
var TagCloud = (function() {
var createTagsArray = function() {
j$("#" + this.cloudId + " a").each(function(index) {
//bla bla
});
}
function TagCloud(cloudId) {
this.cloudId = cloudId;
};
TagCloud.prototype.createTagCloud = function(){
createTagsArray(this.cloudId);
};
return TagCloud;
}());
new TagCloud("tagcloud");
I create TagCloud object. 我创建TagCloud对象。 I create it with closure to have a private function createTagArray . 我使用闭包创建它以拥有私有函数createTagArray 。 However inside this function I would have an access to variable defined in TagCloud constructor - cloudId . 但是,在此函数内部,我可以访问TagCloud构造函数-cloudId中定义的变量。 Of course in this example I cannot get it using this . 当然,在此示例中,我无法使用this获得它。 But is there anyway to get it ? 但是总有办法得到它吗? (I wouldn't like to pass this value as function createTagArray parameter). (我不想将此值作为函数createTagArray参数传递)。
It can be also my mistake in understanding usage of closure, because I've started working with closures. 在理解闭包的用法时,这也是我的错误,因为我已经开始使用闭包。
You cannot access the variable through closure, for that you would need to define the createTagsArray
function within the TagCloud
constructor function - and then you couldn't access it from the createTagCloud
method any more without making it public. 您无法通过闭包访问变量,因为您将需要在TagCloud
构造函数中定义createTagsArray
函数-然后您将无法通过createTagCloud
方法访问该变量而不将其公开。
But I don't think you want access to the variable anyway. 但是我不认为您仍然想要访问该变量。 You want access to the .cloudId
property of your instance, and for that you need the instance. 您想要访问实例的.cloudId
属性,为此您需要实例。
Passing it - either the property value or the complete instance - as a parameter would be preferred actually. 实际上,最好将其(属性值或完整实例)作为参数传递。 There's nothing wrong with that: 这没有错:
var createTagsArray = function(cloudId) {
j$("#" + cloudId + " a").each(function(index) {
//bla bla
});
}
TagCloud.prototype.createTagCloud = function(){
createTagsArray(this.cloudId);
};
And using call
you could even pass the instance itself so that you can access it as this
: 并使用call
,你甚至可以通过实例本身,这样就可以访问它this
:
var createTagsArray = function() {
j$("#" + this.cloudId + " a").each(function(index) {
//bla bla
});
}
TagCloud.prototype.createTagCloud = function(){
createTagsArray.call(this);
};
From there you could even easily switch (back and forth) to a semi-private method: 从那里,您甚至可以轻松地(来回)切换到半私有方法:
TagCloud.prototype._createTagsArray = function() {
j$("#" + this.cloudId + " a").each(function(index) {
//bla bla
});
}
TagCloud.prototype.createTagCloud = function(){
this._createTagsArray();
};
Try following code: 尝试以下代码:
function TagCloud(cloudId) { var self = this; self.createTagsArray = function() { console.log(this); } self.cloudId = cloudId; self.createTagCloud = function() { self.createTagsArray() } return{ "cloudId": cloudId, "createTagCloud": self.createTagCloud } }; var obj = new TagCloud("tagcloud"); obj.createTagCloud(); var obj = new TagCloud("TagCloudTest"); obj.createTagCloud();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.