|
| 1 | +# JavaScript深入之new的模拟实现 |
| 2 | + |
| 3 | +一句话介绍new: |
| 4 | + |
| 5 | +>The new operator creates an instance of a user-defined object type or of one of the built-in object types that has a constructor function. |
| 6 | +
|
| 7 | +依然是来自MDN,之所以不使用中文版的,是因为翻译的没看懂…… |
| 8 | + |
| 9 | +我们自己来翻译下这句话: |
| 10 | + |
| 11 | +new运算符创建了一个用户自定义对象类型或者一个内置对象类型的实例,该实例具有一个constructor函数。 |
| 12 | + |
| 13 | +也许有点晦涩,我们在模拟new之前,先看看new实现了哪些功能。 |
| 14 | + |
| 15 | +举个例子: |
| 16 | + |
| 17 | +```js |
| 18 | +function Person (name, age) { |
| 19 | + this.hobbit = 'game'; |
| 20 | + |
| 21 | + this.name = name; |
| 22 | + this.age = age; |
| 23 | + |
| 24 | +} |
| 25 | + |
| 26 | +Person.prototype.strength = 100; |
| 27 | + |
| 28 | +Person.prototype.sayName = function () { |
| 29 | + console.log(this.name); |
| 30 | +} |
| 31 | + |
| 32 | +var person = new Person('kevin', '18'); |
| 33 | + |
| 34 | +console.log(person.name) // kevin |
| 35 | +console.log(person.hobbit) // game |
| 36 | +console.log(person.strength) // 100 |
| 37 | + |
| 38 | +person.sayName(); // kevin |
| 39 | + |
| 40 | +``` |
| 41 | + |
| 42 | +例子写的可能有些不恰当,但是看效果~ |
| 43 | + |
| 44 | +我们可以看到: |
| 45 | + |
| 46 | +实例person可以 |
| 47 | +1. 访问到Person构造函数里的属性 |
| 48 | +2. 访问到Person.prototype中的属性 |
| 49 | + |
| 50 | +那接下来,我们可以尝试着去模拟一下了。 |
| 51 | + |
| 52 | +因为new是关键字,所以无法直接更改new的效果,所以我们写一个函数,叫做objectFactory,来实现new的效果 |
| 53 | + |
| 54 | +用的时候是这样的: |
| 55 | + |
| 56 | +```js |
| 57 | +function Person () { |
| 58 | + …… |
| 59 | +} |
| 60 | + |
| 61 | +// var person = new Person(……); |
| 62 | +var person = objectFactory(Person, ……) |
| 63 | + |
| 64 | +``` |
| 65 | + |
| 66 | +我们尝试着写第一版: |
| 67 | + |
| 68 | +```js |
| 69 | +// 第一版代码 |
| 70 | +function objectFactory() { |
| 71 | + |
| 72 | + var obj = new Object(), |
| 73 | + |
| 74 | + Constructor = [].shift.call(arguments); |
| 75 | + |
| 76 | + obj.__proto__ = Constructor.prototype; |
| 77 | + |
| 78 | + Constructor.apply(obj, arguments); |
| 79 | + |
| 80 | + return obj; |
| 81 | + |
| 82 | +}; |
| 83 | + |
| 84 | +``` |
| 85 | + |
| 86 | +在这个示例中,我们 |
| 87 | +1. 用new Object()的方式新建了一个对象obj |
| 88 | +2. 取出第一个参数,就是我们要传入的构造函数。此外因为shift会修改原数组,所以arguments会被去除第一个参数 |
| 89 | +3. 将obj的原型指向构造函数,这样obj就可以访问到构造函数原型中的属性 |
| 90 | +4. 使用apply,改变this的指向到新建的对象,这样obj就可以访问到构造函数中的属性 |
| 91 | +5. 返回obj |
| 92 | + |
| 93 | +如果对原型链这部分不是很清楚,可以看《JavaScript深入之从原型到原型链》 |
| 94 | +如果对apply这部分不是很清楚,可以看《JavaScript深入之call和apply的模拟实现》 |
| 95 | + |
| 96 | +复制以下的代码,到浏览器中,我们可以做一下测试: |
| 97 | + |
| 98 | +```js |
| 99 | +function Person (name, age) { |
| 100 | + this.hobbit = 'game'; |
| 101 | + |
| 102 | + this.name = name; |
| 103 | + this.age = age; |
| 104 | + |
| 105 | +} |
| 106 | + |
| 107 | +Person.prototype.strength = 100; |
| 108 | + |
| 109 | +Person.prototype.sayName = function () { |
| 110 | + console.log(this.name); |
| 111 | +} |
| 112 | + |
| 113 | +function objectFactory() { |
| 114 | + |
| 115 | + var obj = new Object(), |
| 116 | + |
| 117 | + Constructor = [].shift.call(arguments); |
| 118 | + |
| 119 | + obj.__proto__ = Constructor.prototype; |
| 120 | + |
| 121 | + Constructor.apply(obj, arguments); |
| 122 | + |
| 123 | + return obj; |
| 124 | + |
| 125 | +}; |
| 126 | + |
| 127 | +var person = objectFactory(Person, 'kevin', '18') |
| 128 | + |
| 129 | +console.log(person.name) // kevin |
| 130 | +console.log(person.hobbit) // game |
| 131 | +console.log(person.strength) // 100 |
| 132 | + |
| 133 | +person.sayName(); // kevin |
| 134 | +``` |
| 135 | + |
| 136 | +[]~( ̄▽ ̄)~** |
| 137 | + |
| 138 | +接下来我们再来看一种情况,假如构造函数有返回值,举个例子: |
| 139 | + |
| 140 | +```js |
| 141 | +function Person (name, age) { |
| 142 | + |
| 143 | + this.strength = 10; |
| 144 | + this.age = age; |
| 145 | + |
| 146 | + return { |
| 147 | + name: name, |
| 148 | + hobbit: 'game' |
| 149 | + } |
| 150 | + |
| 151 | +} |
| 152 | + |
| 153 | +var person = new Person('kevin', '18'); |
| 154 | + |
| 155 | +console.log(person.name) // kevin |
| 156 | +console.log(person.hobbit) // game |
| 157 | +console.log(person.strength) // undefined |
| 158 | +console.log(person.age) // undefined |
| 159 | + |
| 160 | +``` |
| 161 | + |
| 162 | +在这个例子中,构造函数返回了一个对象,在实例person中只能访问返回的对象中的属性 |
| 163 | + |
| 164 | +而且还要注意一点,在这里我们返回了一个对象,假如我们只是返回一个基本类型的值呢? |
| 165 | + |
| 166 | +再举个例子: |
| 167 | + |
| 168 | +```js |
| 169 | +function Person (name, age) { |
| 170 | + |
| 171 | + this.strength = 10; |
| 172 | + this.age = age; |
| 173 | + |
| 174 | + return 'handsome boy'; |
| 175 | + |
| 176 | +} |
| 177 | + |
| 178 | +var person = new Person('kevin', '18'); |
| 179 | + |
| 180 | +console.log(person.name) // undefined |
| 181 | +console.log(person.hobbit) // undefined |
| 182 | +console.log(person.strength) // 10 |
| 183 | +console.log(person.age) // 18 |
| 184 | + |
| 185 | +``` |
| 186 | + |
| 187 | +结果完全颠倒过来,这次尽管有返回值,但是相当于没有返回值进行处理。 |
| 188 | + |
| 189 | +所以我们还需要判断返回的值是不是一个对象,如果是一个对象,我们就返回这个对象,如果没有,我们该返回什么返回什么 |
| 190 | + |
| 191 | +再来看第二版的代码,就是最后一版的代码: |
| 192 | + |
| 193 | +```js |
| 194 | +// 第二版的代码 |
| 195 | +function objectFactory() { |
| 196 | + |
| 197 | + var obj = new Object(), |
| 198 | + |
| 199 | + Constructor = [].shift.call(arguments); |
| 200 | + |
| 201 | + obj.__proto__ = Constructor.prototype; |
| 202 | + |
| 203 | + var ret = Constructor.apply(obj, arguments); |
| 204 | + |
| 205 | + return typeof ret === 'object' ? ret : obj; |
| 206 | + |
| 207 | +}; |
| 208 | +``` |
| 209 | + |
| 210 | +实际上这篇文章只是希望大家通过模拟new的实现来了解new的原理,模拟也有一些细节的地方没有注意,比如构造函数返回了一个null…… |
| 211 | + |
0 commit comments