0

My understanding is that things on the prototype chain are only created once per object and that to create instance variables, one must use this. Is this correct?

For example:

function Foo () {
    this.some_instance = "hello";
}
Foo.prototype.some_static = "hi";

Implementation

var foo1 = new Foo();

var foo2 = new Foo(); 

Making a foo1 and a foo2 will create two values of hello but only one of hi.

Is this correct?

employee-0
  • 1,023
  • 1
  • 9
  • 19
  • 1
    Yes the `some_static` variable will not be recreated for each instance and will exist on the prototype chain – megawac Nov 03 '13 at 01:23
  • This sort of thing could be tested very easily. – Paul Draper Nov 03 '13 at 01:24
  • 1
    +1 @megawac, [this](http://stackoverflow.com/questions/6986794/why-are-instance-methods-defined-in-the-prototype-but-instance-fields-are-define) would be a helpful read – hiattp Nov 03 '13 at 01:24
  • the bigger question, was how to create instance variables with out using this, is it possible? I have a framework that create "classes" that I'm trying to update. – employee-0 Nov 03 '13 at 01:25
  • Which library are you trying to update – megawac Nov 03 '13 at 01:26
  • To create instance variables (properties) you need to say `someObjectReference.someInstanceVariable = something`. Inside a constructor function, `this` is the reference that you have to the object in question, so why don't you want to use it? – nnnnnn Nov 03 '13 at 01:26
  • will be the same if you create `foo1` and `foo2` after you add the new protoype as in order shown above – charlietfl Nov 03 '13 at 01:26
  • What's the difference here? Changing the value of `some_static` on one instance does not affect the other instance. – Kendall Frey Nov 03 '13 at 01:28
  • @hiattp's comment makes this clearer. – Kendall Frey Nov 03 '13 at 01:29
  • @hiattp - i know that already, but thanks. – employee-0 Nov 03 '13 at 01:29
  • @nnnn - I take an object literal and create and object, but I have no way to explictly state my instance variables. This is a new question all together, I'll post the code in just a sec. – employee-0 Nov 03 '13 at 01:31
  • If you're using an object literal you can create instance variables (properties) directly in the literal. But yes, it sounds like a completely rephrased question would help. – nnnnnn Nov 03 '13 at 01:33
  • @nnn - please see http://stackoverflow.com/questions/19748947/how-to-create-instance-variables-explicitly ... and let me know what info. I need to add. – employee-0 Nov 03 '13 at 01:37

2 Answers2

3

Short answer: Yes.

Proof:

function Foo () {
    this.some_instance = "hello";
}

var foo1 = new Foo();
Foo.prototype.some_static = "hi"; // in between
var foo2 = new Foo();

foo1.some_static === foo2.some_static; // return true
Broxzier
  • 2,909
  • 17
  • 36
  • 1
    Thats a poor test a better test would be `Foo.prototype.some_static = {a:1}` – megawac Nov 03 '13 at 01:38
  • I agree with megawac, that only proves that "hi"==="hi" you can define some_static as this.some_static, assign "hi" to it and the === will still be true. As megawac pointed out you can notice the sharing when you have mutable objects as your property value because assigning values to a property will cause JS to not look up on the prototype chain. Instead it'll just create that property on the instance and assign the value to it. `foo1.some_static=...` will create some_static on foo1 instance. http://stackoverflow.com/a/16063711/1641941 – HMR Nov 03 '13 at 02:11
2

Well, making a foo1 and a foo2 won't create any values of "hi". The "some_static" variable with the value "hi" was already created, in the Foo prototype object, before you created foo1 and foo2.

When you create objects that use the Foo prototype, and you reference their "some_static" property, it will look in the objects themselves first. If the objects don't have a "some_static" property, it will look for it in their prototype object.

But yes, if you want to create a property that's unique to each object that uses the same prototype, you should set it on the object itself, not the prototype. You can do that by setting "this.some_instance" from inside one of the object's methods, or by setting "foo1.some_instance" from outside.

JW.
  • 50,691
  • 36
  • 115
  • 143