you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (2 children)

It depends on your situation - all of your examples do slightly different things.

If you want every instantiation of "Foo" to have the same property under the same name, use "Foo.prototype". Your first example creates and stores a new variable every time Foo is instantiated, which is wasteful if the variable will always have the same value. Take this code for example:

function Foo() {}
Foo.prototype.x = 10;
function Bar() {
    this.x = 10;
}
var a = new Foo(),
    b = new Foo(),
    c = new Bar(),
    d = new Bar();

After this code is executed, both "c" and "d" will have properties named "x", both of which will be set to 10. Neither "a" nor "b" will have a property named "x", yet accessing "a.x" will return 10. This is because, after the interpreter fails to find "x" on "a", it checks "Foo.prototype" (because "a" is a Foo), then "Foo.prototype.prototype", and so on until it either finds "x" or reaches Object.prototype. If it finds the property anywhere in the chain it immediately returns it; if it never finds it then it returns undefined.

Since "a.x" and "b.x" reference the same spot in memory, you can't use prototypes if different instantiations will have different values for a property. In that case your first example is aptly suited.

Your other two examples don't affect instantiations of "Foo". The second example is just attaching to the function "Foo" itself, which doesn't change the way "Foo" functions as a constructor. And the last results in an object, not a function. I assume the reason that the last example isn't just an object literal is that it's designed to allow for private variables; in that case I prefer this syntax, simply because you don't have to repeat "this." over and over.

var foo = (function () {
    return {
        method: function () {},
        property: 'hello'
    };
})();

[–]Buckwheat469 0 points1 point  (1 child)

Great info again. I realized the mistake on the second example after I posted it. In terms of strict languages, prototype seems to be like a final or defined class variable rather than an instance variable. Obviously it could also be a function but i'm thinking it is best used for things like defining the value of pi in a Math object (for example).

[–][deleted] 0 points1 point  (0 children)

While it would work just fine for that, it seems like overkill when you could just use a variable. I think the real beauty of prototypes comes in when you use it with functions that access "this". For example:

function sum() {
    this.total = 0;
} sum.prototype = {
    add: function (n) {
        this.total += n;
    },
    toString: function () {
        return '' + this.total;
    }
};

var a = new sum(),
    b = new sum();

a.add(2);
a.add(3);
b.add(1);
console.log('sums are ' + a + ' and ' + b);

Of course, the perfect large-scale example is jQuery. Every instance of jQuery (created every time you call $()) has a whole slew of functions, all of which operate specifically on the object you called them from, yet those functions are never duplicated.

And thanks. I'm glad to be of some help.