學習記錄一下 Javascript 對象原型模式(prototype)
Prototype 主要用于在多個實例之間共享對象的屬性和方法:
2. 它可實現類的繼承;
例如:
function Person(name){
this.name = name;
}
Person.prototype.age = 17;
Person.prototype.sayName = function()
{
console.log(this.name)
};
let person1 = new Person("Jacky"); let person2 = new Person("Json"); person1.age = 20; console.log(person1.age);//20 console.log(person1.name);//Jacky console.log(person2.age);//17 person1.address = "road city";//可以有自已的擴展屬性 console.log(person1.hasOwnProperty("address"));//true console.log(person1.hasOwnProperty("name"));//false; person1.name = "Wang"; console.log(person1.hasOwnProperty("name"));//true;