简体   繁体   English

如何在函数内访问父对象的变量

[英]how to access variables of parent object within function

I need to use functions within a prototype, that can access the parent objects variables without being executed at the time of creation of the object. 我需要在原型中使用函数,这些函数可以访问父对象变量,而无需在创建对象时执行。

var Foo=function(e,d) {
    this.b=e;
    this.c=d;
    // this.d will be this.b + this.c
}

window.onload=function() {
    var e=4,d=7;
    var bas=new Foo(e,d);

    // random things happen

    Foo.prototype.addFunc=function() {
        // want to create d (b + c), but can't access them
    }();
    console.log(bas.d); // should be 11
}

You cannot call the method of the prototype like this. 你不能像这样调用原型的方法。 You can only call the method on an instance 您只能在实例上调用该方法

var Foo=function(e,d) {
   this.b=e;
   this.c=d;
};

var e=4,d=7;
var bas=new Foo(e,d);

// random things happen

Foo.prototype.addFunc=function() {
    this.d = this.b + this.c;
};
bas.addFunc();
alert(bas.d); // is 11

Technically speaking, you could call the function like this ... if it returned a function itself which would then be assigned to the prototype. 从技术上讲,您可以像这样调用函数...如果它返回一个函数本身,然后将其分配给原型。 But this is certainly not what you wanted 但这肯定不是你想要的

To address your comment on calling addFunc on all instances in one statement: 要解决在一个语句中对所有实例调用addFunc的注释:

To call a certain function on all instances you have to keep track of all the instances. 要在所有实例上调用某个函数,您必须跟踪所有实例。 This can be done in the Object definition (Foo constructor). 这可以在Object定义(Foo构造函数)中完成。

To have an Object definition (constructor function) keep track of all it's instances is a bit complicated. 要让Object定义(构造函数)跟踪它的所有实例有点复杂。

If in a function I create 100 Foo instances and the function exits then these instances would go out of scope. 如果在函数中我创建了100个Foo实例并且函数退出,则这些实例将超出范围。 But because Foo would keep track of it's instances it has a reference to these instances and therefor even after the function exits these instances will not be garbage collected (go out of scope). 但是因为Foo会跟踪它的实例,它会引用这些实例,因此即使在函数退出后这些实例也不会被垃圾收集(超出范围)。

To indicate to Foo that these instances are no longer needed you have to explicitly call destroy on the instances so a reference to that instance is no longer kept in Foo._instances. 要向Foo指示不再需要这些实例,您必须在实例上显式调用destroy,以便不再在Foo._instances中保留对该实例的引用。

Here is some code to demonstrate: 以下是一些演示代码:

//Object definition of Foo (constructor function)
var Foo=function(e,d) {
    this.b=e;
    this.c=d;
    //saving this instance in Foo._instances
    this._instanceId=Foo._instanceCount;
    Foo._instances[this._instanceId]=this;
    Foo._instanceCount++;
}
//properties of the Object definition
Foo.allAddFunc=function(){
  var thing;
  for(thing in Foo._instances){
    if(Foo._instances.hasOwnProperty(thing)){
      Foo._instances[thing].addFunc();
    }
  }
};
//container for all instances of Foo
Foo._instances={};
//does not refllect the actual count
// used to create _instanceId
Foo._instanceCount=0;
//prototype of Foo (used for foo instances)
Foo.prototype.addFunc=function() {
  this.d=this.b+this.c;
};
//when a Foo instance is no longer needed
//  you should call destroy on it
Foo.prototype.destroy=function(){
  delete Foo._instances[this._instanceId];
};

(function() {
    var bas=new Array(10),i=0,len=bas.length;
    //create instances
    for(;i<len;i++){
      bas[i]=new Foo(i,i*2);
    };
    //do something with all instances
    Foo.allAddFunc();
    //bas[] will go out of scope but all instances of
    // Foo will stay in Foo_instances, have to explicitely
    // destroy these instances
    for(i=0,len=bas.length;i<len;i++){
      console.log("bas.d at "+i+":",bas[i].d);
      //Foo instance at bas[i] no longer needed
      //  destroy it
      bas[i].destroy();
    };    
}());

About prototype: https://stackoverflow.com/a/16063711/1641941 关于原型: https//stackoverflow.com/a/16063711/1641941

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM