1

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
}
2
  • Have you tried this.b and this.c within addFunc? Commented Nov 6, 2013 at 12:58
  • Yes, I have tried using this.b. It returns undefined. Commented Nov 6, 2013 at 13:01

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

So if I have 100bas', I have to loop through them? Is that the only solution?
Yes, if you don't want to initially put the add statement into the constructor
I didn't even notice the () to execute the prototype function in the question. Nice catch!
@ChristophBühler Would you like to call the addFunc of all Foo instances in one statement?
@HMR Yes, that's what I want.
1

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

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.

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

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

About prototype: https://stackoverflow.com/a/16063711/1641941

1 Comment

Thank you. This is the clean solution, I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.