创建对象
工厂模式
1 2 3 4 5 6 7 8 9 10 11 12
| function Person1(name) { let o = new Object() o.name = name o.sayName = function () {} return o } let person1 = Person1('Orion') Person1.prototype person1.__proto__ person1 instanceof Person1 person1.constructor === Person1
|
构造函数 constructor
1 2 3 4 5 6 7 8 9
| function Person2 (name) { this.name = name this.sayName = function () {} } var person2 = new Person2('Orion') Person2.prototype person2.__proto__ person2 instanceof Person2 person2.constructor === Person2
|
原型模式
1 2 3 4 5 6 7 8 9
| function Person3 () {} Person3.prototype.name = 'Orion' Person3.prototype.sayName = function () {} let person3 = new Person3() Person3.prototype person3.__proto__ person3 instanceof Person3 person3.constructor === Person3
|
对象字面量封装
1 2 3 4 5 6 7 8 9 10 11 12
| function Person4 () {}
Person4.prototype = { name: 'Orion', sayName: function () {} } let person4 = new Person4() Person4.prototype person4.__proto__ person4 instanceof Person4 person4.constructor === Person4
|
-
Next Post
前端字体
-
Previous Post
WebStorm文件模板