如何在函数内访问父对象的变量 [英] how to access variables of parent object within function

查看:42
本文介绍了如何在函数内访问父对象的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在原型中使用函数,该函数可以访问父对象变量,而无需在创建对象时执行.

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
}

推荐答案

解决您对在一个语句中对所有实例调用 addFunc 的评论:

To address your comment on calling addFunc on all instances in one statement:

要在所有实例上调用某个函数,您必须跟踪所有实例.这可以在对象定义(Foo 构造函数)中完成.

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).

让一个对象定义(构造函数)跟踪它的所有实例有点复杂.

To have an Object definition (constructor function) keep track of all it's instances is a bit complicated.

如果在一个函数中我创建了 100 个 Foo 实例并且该函数退出,那么这些实例将超出范围.但是因为 Foo 会跟踪它的实例,所以它有对这些实例的引用,因此即使在函数退出后,这些实例也不会被垃圾收集(超出范围).

If in a function I create 100 Foo instances and the function exits then these instances would go out of scope. 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 表明不再需要这些实例,您必须在实例上显式调用 destroy 以便对该实例的引用不再保留在 Foo._instances 中.

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.

这里有一些代码来演示:

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();
    };    
}());

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

这篇关于如何在函数内访问父对象的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆