原型属性通常出现在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