プロトタイプ プロパティは、通常、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
したがって、プレーン オブジェクトにはプロトタイプがありません。コンストラクターとして機能する関数には、プロトタイプがあります。これらのプロトタイプは、各構成で作成されたインスタンスを満たすために使用されます。
それが役立つことを願っています:)