他の答えを明確にするために; 関数宣言、関数によって作成されたインスタンス、および関数のプロトタイプには違いがあります。次のコードがそれを示していることを願っています。
//declararion:
function Person(name){
this.name=name
}
// sayName is a method of a person instance like jon.sayName
Person.prototype.sayName=function(){
console.log("Hi, I'm "+Person.formatName(this.name));
};
// static property of Person to format name
Person.formatName=function(name){
return name.toLowerCase().replace(/\b\w/g,function(){
return arguments[0].toUpperCase();
});
};
// now we create an instance of person
var jon = new Person("jon holt");
jon.sayName();//=Hi, I'm Jon Holt
// next line's output:
//["prototype", "formatName", "length", "name", "arguments", "caller"]
console.log(Object.getOwnPropertyNames(Person));
// name in Person isn't the same as name in jon
console.log(Person.name);//=Person
// next line's output: ["name"], name here would be jon holt
console.log(Object.getOwnPropertyNames(jon));
// next line's output: ["constructor", "sayName"]
console.log(Object.getOwnPropertyNames(Person.prototype));
関数コンストラクター、プロトタイプ、および継承を使用するいくつかの方法へのリンクを次に示します。