原型屬性通常出現在Function對象中。這個原型應該是一個對象,並且該對象用於定義用構造函數創建的對象的屬性。
// Plain object, no prototype property here.
var plainObject = {one: 1, two: 2};
// Constructor, a prototype property will be created by default
var someConstruct = function() {
// Constructor property
someConstruct.constructProp = "Some value";
// Constructor's prototype method
someConstruct.prototype.hello = function() {
return "Hello world!";
}
};
// Another constructor's prototype method
someConstruct.prototype.usefulMethod = function() {
return "Useful string";
}
var someInstance = new someConstruct();
console.log(someInstance.hello()); // => Hello world!
console.log(someInstance.usefulMethod()); // => Useful string
console.log(someConstruct.constructProp); // => Some value
console.log(someConstruct.prototype); // => {usefulMethod: function, hello: function}
console.log(plainObject.prototype); // => undefined
所以,普通物體沒有原型。 作爲構造函數的函數確實有原型。這些原型用於填充每個構造創建的實例。
希望幫助:)
相關:http://stackoverflow.com/q/ 7015693/989121 – georg 2013-03-18 07:58:38
簡短回答:你不能 – slebetman 2013-03-18 08:35:09
可能的重複e的http://stackoverflow.com/questions/9959727/java-script-what-is-the-difference-between-proto-and-prototype或http://stackoverflow.com/questions/572897/how-does- javascript-prototype-work?lq = 1或http://stackoverflow.com/questions/650764/how-does-proto-differ-from-constructor-prototype?rq=1或http://stackoverflow.com/questions/ 9451881/prototype-vs-prototype-what-is-the-difference-mycons-proto-myco/9451979#9451979 – 2013-03-18 08:56:35