简体   繁体   English

用闭包创建的私有函数如何访问构造函数中定义的变量?

[英]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.

相关问题 可以原型方法访问构造函数的上下文(闭包) - can prototype method access constructor function's context (closure) 如何在JS闭包函数中访问变量 - How to access variable in JS closure function 量角器:无法访问父函数中定义的闭包中的变量 - Protractor: Can not access variables in closure defined in parent function 如何从闭包中的函数内部访问闭包中的变量? - How can I access variables in a closure from inside of a function in that closure? 访问闭包范围内的私有(局部)变量 - Access private (local) variable inside a closure scope 如何通过Google Closure Compiler将功能标记为“私有”以重命名? - How i can mark function as “private” to renaming it by Google Closure Compiler? 在闭包(?)函数中清除私有变量的覆盖 - Clean overwrite of private variable inside of closure (?) function ES5构造函数中定义的私有变量之谜 - ES5 puzzle of private variable defined in constructor 使用闭包还是常规构造函数来创建私有属性? - Closure versus regular constructor function for creating private properties? 如何在构造函数中使用公共公用函数访问私有变量 - How to access a private variables using a common public function within the constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM