new操作符
一、是什么
在JavaScript
中,new
操作符用于创建一个给定构造函数的实例对象
例子
function Person(name, age){
this.name = name;
this.age = age;
}
Person.prototype.sayName = function () {
console.log(this.name)
}
const person1 = new Person('Tom', 20)
console.log(person1) // Person {name: "Tom", age: 20}
person1.sayName() // 'Tom'
从上面可以看到:
new
通过构造函数Person
创建出来的实例可以访问到构造函数中的属性new
通过构造函数Person
创建出来的实例可以访问到构造函数原型链中的属性(即实例与构造函数通过原型链连接了起来)
现在在构建函数中显式加上返回值,并且这个返回值是一个原始类型
function Test(name) {
this.name = name
return 1
}
const t = new Test('xxx')
console.log(t) // Test {name: 'xxx'}
console.log(t.name) // 'xxx'
可以发现,构造函数中返回一个原始值,然而这个返回值并没有作用
下面在构造函数中返回一个对象
function Test(name) {
this.name = name
console.log(this) // Test { name: 'xxx' }
return { age: 26 }
}
const t = new Test('xxx')
console.log(t) // { age: 26 }
console.log(t.name) // 'undefined'
从上面可以发现,构造函数如果返回值为一个对象,那么这个返回值会被正常使用
二、流程
从上面介绍中,我们可以看到new
关键字主要做了以下的工作:
创建一个新的对象
obj
将对象与构建函数通过原型链连接起来
将构建函数中的
this
绑定到新建的对象obj
上根据构建函数返回类型作判断,如果是原始值则被忽略,如果是返回对象,需要正常处理
举个例子:
function Person(name, age){
this.name = name;
this.age = age;
}
const person1 = new Person('Tom', 20)
console.log(person1) // Person {name: "Tom", age: 20}
person1.sayName() // 'Tom'
流程图如下:
三、手写new操作符
现在我们已经清楚地掌握了new
的执行过程
那么我们就动手来实现一下new
function mynew(func, ...args) {
// 创建一个新的对象,并将它的原型设置为构造函数的 prototype 属性
const obj = Object.create(func.prototype);
// 调用构造函数,将 this 绑定到新创建的对象
const result = Func.apply(obj, args);
// 如果构造函数返回了一个对象,那么返回这个对象
// 否则,返回新创建的对象
return result instanceof Object ? result : obj;
}
测试一下
function mynew(func, ...args) {
const obj = Object.create(func.prototype);
const result = func.apply(obj, args);
return result instanceof Object ? result : obj;
}
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.say = function () {
console.log(this.name)
}
let p = mynew(Person, "huihui", 123)
console.log(p) // Person {name: "huihui", age: 123}
p.say() // huihui
可以发现,代码虽然很短,但是能够模拟实现new
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果