JavaScript prototypes [duplicate]
Possible Duplicate:
JavaScript: Class.method vs. Class.prototype.method
What's the difference between creating a prototype like this:
Date.foo = function(bar) {
alert(bar);
};
And this:
Date.prototype.foo = function(bar) {
alert(bar);
};
Why/when should I use either?
in the first example, foo is a constructor method, its like a 'static' method in java. The second is like defining a method foo on a class -- it is scoped to the instance.
you would access the first like
Date.foo()
and the second like
Date d = new Date()
d.foo()
or in another method on an instance of Date like
this.foo()
精彩评论