|
58 | 58 |
|
59 | 59 |
|
60 | 60 | //【组合模式】 |
61 | | -function Person(name, age) { |
62 | | - this.name = name; |
63 | | - this.age = age; |
64 | | - this.friends = ['Shelby', 'Court']; |
65 | | -} |
66 | | - |
67 | | -Person.prototype = { |
68 | | - constructor: Person, |
69 | | - sayName: function() { |
70 | | - console.log(this.name); |
71 | | - } |
72 | | -}; |
73 | | - |
74 | | -var person1 = new Person('Nicholas', 29), |
75 | | - person2 = new Person('Greg', 27); |
76 | | - |
77 | | -person1.friends.push('Van'); |
78 | | -console.log(person1.friends); |
79 | | -console.log(person2.friends); |
80 | | -console.log(person1.friends === person2.friends); //false |
81 | | -console.log(person1.sayName === person2.sayName); //true |
| 61 | +// function Person(name, age) { |
| 62 | +// this.name = name; |
| 63 | +// this.age = age; |
| 64 | +// this.friends = ['Shelby', 'Court']; |
| 65 | +// } |
| 66 | + |
| 67 | +// Person.prototype = { |
| 68 | +// constructor: Person, |
| 69 | +// sayName: function() { |
| 70 | +// console.log(this.name); |
| 71 | +// } |
| 72 | +// }; |
| 73 | + |
| 74 | +// var person1 = new Person('Nicholas', 29), |
| 75 | +// person2 = new Person('Greg', 27); |
| 76 | + |
| 77 | +// person1.friends.push('Van'); |
| 78 | +// console.log(person1.friends); |
| 79 | +// console.log(person2.friends); |
| 80 | +// console.log(person1.friends === person2.friends); //false |
| 81 | +// console.log(person1.sayName === person2.sayName); //true |
82 | 82 | //【组合模式】 |
83 | 83 |
|
84 | 84 |
|
85 | 85 |
|
86 | 86 | //【动态原型模式】 |
| 87 | +// function Person(name, age) { |
| 88 | +// this.name = name; |
| 89 | +// this.age = age; |
| 90 | + |
| 91 | +// if (typeof this.sayName != 'function') { |
| 92 | +// Person.prototype.sayName = function() { |
| 93 | +// console.log(this.name); |
| 94 | +// } |
| 95 | +// } |
| 96 | +// } |
87 | 97 | //【动态原型模式】 |
88 | 98 |
|
89 | 99 |
|
90 | 100 |
|
91 | 101 | //【寄生构造函数模式】 |
| 102 | +// function SpecialArray() { |
| 103 | +// var values = new Array(); |
| 104 | + |
| 105 | +// values.push.apply(values, arguments); |
| 106 | + |
| 107 | +// values.toPipedString = function() { |
| 108 | +// return this.join('|'); |
| 109 | +// }; |
| 110 | + |
| 111 | +// return values; |
| 112 | +// } |
| 113 | + |
| 114 | +// var colors = new SpecialArray('red', 'blue', 'green'); |
| 115 | +// console.log(colors.toPipedString()); |
92 | 116 | //【寄生构造函数模式】 |
93 | 117 |
|
94 | 118 |
|
|
0 commit comments