工廠模式:用函數來封裝,以特定介面來創建對象的細節。 console.log(person1 instanceof person); // false console.log(person1 instanceof Object);//true console.log(person instanceo ...
工廠模式:用函數來封裝,以特定介面來創建對象的細節。
創建的對象是Object類型
function person(name, age , job){ var o =new Object() ; o.name = name; o.age = age ; o.job = job ; o.sayName = function(){ alert(this.name); }; return o ; var person1 = new person("cancan",21,"student"); var person2 = new person("xiaoma",45,"teacher");
console.log(person1 instanceof person); // false
console.log(person1 instanceof Object);//true
console.log(person instanceof person); //true
構造函數模式:構造函數可以創建特定類型的對象,自定義對象的屬性和方法
function person(name,age,job){ this.name = name ; this.age = age ; this.job = job ; this.sayName = function(){ alert(this.name); }; } var person1 = new person("cancan",21,"student"); var person2 =new person("xiaoma",45,"teacher");
console.log(person1 instanceof person); // true
console.log(person instanceof Object); // true