1 function createPerson(name, age) { 2 let obj = new Object(); 3 obj.name = name; 4 obj.age = age; 5 obj.sayHi = function() { 6 console.info('hi, I am ' + this.name); 7 }; 8 return obj; 9 } 10 let p1 = createPerson('pig', 26); 11 let p2 = createPerson('tug', 27); 12 p1.sayHi(); // hi, I am pig 13 p2.sayHi(); // hi, I am tug
1 function Person(name, age) { 2 this.name = name; 3 this.age = age; 4 this.sayHi = () => { 5 console.info('hi, I am ' + this.name); 6 } 7 } 8 let p1 = new Person('pig', 26); 9 let p2 = new Person('tug', 27); 10 p1.sayHi(); // hi, I am pig 11 p2.sayHi(); // hi, I am tug
1 console.info(p1.sayHi == p2.sayHi) // false
1 function sayHiFn() { 2 console.info('hi, I am ' + this.name); 3 } 4 function Person(name, age) { 5 this.name = name; 6 this.age = age; 7 this.sayHi = sayHiFn; 8 } 9 let p1 = new Person('pig', 26); 10 let p2 = new Person('tug', 27); 11 p1.sayHi(); // hi, I am pig 12 p2.sayHi(); // hi, I am tug 13 console.info(p1.sayHi == p2.sayHi); // true
1 function Person() {} 2 Person.prototype.name = 'pig'; 3 Person.prototype.age = 27; 4 Person.prototype.sayHi = function() { 5 console.info('hi, I am ' + this.name); 6 }; 7 8 let p1 = new Person(); 9 let p2 = new Person(); 10 p1.sayHi(); // hi, I am pig 11 p2.sayHi(); // hi, I am pig 12 console.info(p1.sayHi == p2.sayHi); // true
1 function Person() {} 2 Person.prototype = { 3 name: 'pig', 4 age: 27, 5 sayHi() { 6 console.info('hi, I am ' + this.name); 7 } 8 } 9 let p1 = new Person(); 10 p1.sayHi(); // hi, I am pig
1 console.info(Person.prototype.constructor == Person); // false 2 // 實際上Person.prototype被從新賦值了一個對象,因此其constructor爲Object 3 console.info(Person.prototype.constructor == Object); // true
1 function Person() {} 2 Person.prototype = { 3 constructor: Person, 4 name: 'pig', 5 age: 27, 6 sayHi() { 7 console.info('hi, I am ' + this.name); 8 } 9 } 10 console.info(Person.prototype.constructor == Person); // true
1 let p1 = new Person(); 2 for (const key in p1) { 3 console.info(key); // 依次constructor、name、age、sayHi 4 }
1 function Person() {} 2 Person.prototype = { 3 name: 'pig', 4 age: 27, 5 sayHi() { 6 console.info('hi, I am ' + this.name); 7 } 8 } 9 Object.defineProperty(Person.prototype, 'constructor', { 10 enumberable: false, 11 value: Person 12 }); 13 // 這樣constructor不會被枚舉出來 14 let p1 = new Person(); 15 for (const key in p1) { 16 console.info(key); // 依次name、age、sayHi 17 }
1 function Person() {} 2 Person.prototype = { 3 name: 'pig', 4 age: 27, 5 friend: ['小馬', '小魚', '佩奇'], 6 sayHi() { 7 console.info('hi, I am ' + this.name); 8 } 9 } 10 Object.defineProperty(Person.prototype, 'constructor', { 11 enumberable: false, 12 value: Person 13 }); 14 let p1 = new Person(); 15 p1.friend.push('小羊'); 16 console.info(p1.friend); // ["小馬", "小魚", "佩奇", "小羊"] 17 let p2 = new Person(); 18 console.info(p2.friend); // ["小馬", "小魚", "佩奇", "小羊"]
1 function Father() { 2 this.firstName = 'wang'; 3 this.action = function () { 4 console.info('super'); 5 }; 6 } 7 Father.prototype.sayHi = function () { 8 console.info('hi'); 9 }; 10 function Son() {} 11 // 子類的原型指向父類的實例(這樣能包含父類的屬性及父類原型上的屬性) 12 Son.prototype = new Father(); 13 Son.prototype.constructor = Son; // 須要手動將constructor指回來,否則構造函數指向父類 14 // 子類不只能繼承父類原型上的屬性和方法,也能繼承父類自己的屬性 15 let s = new Son(); 16 s.sayHi(); // hi 17 // 獲取父類自身的屬性和方法 18 console.info(s.firstName); // wang 19 s.action(); // super
1 function Father() { 2 this.firstName = 'wang'; 3 this.friend = ['小馬', '小魚', '佩奇']; 4 this.action = function () { 5 console.info('super'); 6 }; 7 } 8 Father.prototype.sayHi = function () { 9 console.info('hi'); 10 }; 11 function Son() {} 12 // 子類的原型指向父類的原型 13 Son.prototype = new Father(); 14 Son.prototype.constructor = Son; // 須要手動將constructor指回來,否則構造函數指向父類 15 // 子類不只能繼承父類原型上的屬性和方法,也能繼承父類自己的屬性 16 let s1 = new Son(); 17 s1.friend.push('小羊'); 18 console.info(s1.friend); // ["小馬", "小魚", "佩奇", "小羊"] 19 let s2 = new Son(); 20 console.info(s2.friend); // ["小馬", "小魚", "佩奇", "小羊"]
1 function Father() { 2 this.firstName = 'wang'; 3 this.friend = ['小馬', '小魚', '佩奇']; 4 this.action = function () { 5 console.info('super'); 6 }; 7 } 8 Father.prototype.sayHi = function () { 9 console.info('hi'); 10 }; 11 function Son() { 12 Father.call(this); // 至關於把父類的代碼在子類中執行了一遍 13 } 14 let s1 = new Son(); 15 s1.friend.push('小羊'); 16 console.info(s1.friend); // ["小馬", "小魚", "佩奇", "小羊"] 17 let s2 = new Son(); 18 // 屬性不會共享 19 console.info(s2.friend); // ["小馬", "小魚", "佩奇"] 20 // 方法也是獨立的 21 console.info(s1.action == s2.action); // false 22 console.info(s1.sayHi); // undefined
1 function Father(firstName) { 2 this.firstName = firstName; 3 this.friend = ['小馬', '小魚', '佩奇']; 4 } 5 Father.prototype.sayHi = function () { 6 console.info('hi, my firstname is ' + this.firstName); 7 }; 8 function Son(firstName) { 9 Father.call(this, firstName); // 至關於把父類的代碼在子類中執行了一遍 10 } 11 Son.prototype = new Father(); 12 13 let s1 = new Son('wang'); 14 s1.friend.push('小羊'); 15 console.info(s1.friend); // ["小馬", "小魚", "佩奇", "小羊"] 16 let s2 = new Son('zhu'); 17 // 屬性放在實例中不會共享 18 console.info(s2.friend); // ["小馬", "小魚", "佩奇"] 19 // 方法放在原型上實現共享 20 console.info(s1.sayHi == s2.sayHi); // true 21 s1.sayHi(); // hi, my firstname is wang 22 s2.sayHi(); // hi, my firstname is zhu
1 function object(o) { 2 function F() {} 3 F.prototype = o; 4 return new F(); 5 } 6 function createObj(original) { 7 let clone = object(original); // 在一個對象的基礎上建立對象 8 clone.sayHi = function () { 9 // 增長一些方法 10 console.info('hi, I am ' + this.firstName); 11 }; 12 return clone; 13 } 14 15 let Father = { 16 firstName: 'wang', 17 friend: ['小馬', '小魚', '佩奇'], 18 action: function () { 19 console.info('super'); 20 } 21 }; 22 23 let s1 = createObj(Father); 24 s1.friend.push('小羊'); 25 console.info(s1.friend); // ["小馬", "小魚", "佩奇", "小羊"] 26 let s2 = createObj(Father); 27 console.info(s2.friend); // ["小馬", "小魚", "佩奇", "小羊"] 28 s1.sayHi(); // hi, I am wang 29 console.info(s1.sayHi == s2.sayHi); // false
1 function object(o) { 2 function F() {} 3 F.prototype = o; 4 return new F(); 5 } 6 function inheritPrototype(son, father) { 7 let prototype = object(father.prototype); 8 son.prototype = prototype; 9 son.prototype.constructor = son; 10 } 11 12 function Father(firstName) { 13 this.firstName = firstName; 14 this.friend = ['小馬', '小魚', '佩奇']; 15 } 16 Father.prototype.sayHi = function () { 17 console.info('hi, my firstname is ' + this.firstName); 18 }; 19 function Son(firstName) { 20 Father.call(this, firstName); // 相對於把父類的代碼在子類中執行了一遍 21 } 22 inheritPrototype(Son, Father); // 改變子構造函數的原型 23 24 let s1 = new Son('wang'); 25 s1.friend.push('小羊'); 26 console.info(s1.friend); // ["小馬", "小魚", "佩奇", "小羊"] 27 let s2 = new Son('zhu'); 28 console.info(s2.friend); // ["小馬", "小魚", "佩奇"] 29 s1.sayHi(); // hi, my firstname is wang 30 console.info(s1.sayHi == s2.sayHi); // true
1 class Father { 2 constructor(firstName) { 3 this.firstName = firstName; 4 this.friend = ["小馬", "小魚", "佩奇"]; 5 } 6 sayHi() { 7 console.info('hi, my firstname is ' + this.firstName); 8 } 9 } 10 class Son extends Father { 11 constructor(firstName) { 12 super(firstName); 13 } 14 hello() { 15 console.info('hello, ' + this.firstName); 16 } 17 } 18 let p1 = new Son('wang'); 19 p1.sayHi(); // hi, my firstname is wang 20 p1.friend.push('小羊'); 21 console.info(p1.friend); // ["小馬", "小魚", "佩奇", "小羊"] 22 let p2 = new Son('zhu'); 23 p2.sayHi(); // hi, my firstname is zhu 24 console.info(p2.friend); // ["小馬", "小魚", "佩奇"] 25 console.info(p1.sayHi === p2.sayHi); // true
《JavaScript高級程序設計》app