问题
Possible Duplicate:
How does JavaScript .prototype work?
What is the use of prototype property when properties can be added to object even without it?
var o = {};
o.x = 5;
o.y = test;
test = new function(){ alert("hello"); };
回答1:
Adding a method / property to a prototype is adding it to all objects with that prototype in their prototype chain.
Your code is adding a method/property to a single instance.
To make use of prototypes you need to create your objects using new. If you create an object via an object literal you aren't specifying the prototype for the object, as far as I know you can't set the prototype retrospectively.
回答2:
You can use it to create new methods for an existing object.
String.prototype.displayFirstCharacter = function(){
alert(this.substr(0,1));
}
"my string, first char should be 'm'".displayFirstCharacter();
来源:https://stackoverflow.com/questions/7165109/what-is-the-use-of-prototype-property-in-javascript