new的模拟实现

实现目标:

  1. 创建新的对象
  2. 新对象属性有构造函数中this绑定的属性
  3. 新对象可以访问构造函数原型链上的属性和方法
  4. 如果构造函数返回了一个对象,实例化的对象只能访问返回的对象中的属性
let newF = function(Constructor, ...rest) {
    const obj = Object.create(Constructor.prototype)

    const res = Constructor.apply(obj, rest)

    // && 当结果为真时,返回第二个为真的值, 当结果为假时,返回第一个为假的值
    return (typeof res === 'object' && res) || obj
}

测试:

function Obj(name) {
  this.name = name
}

Obj.prototype.setName = function(name) {
  this.name = name
}
Obj.prototype.getName = function() {
  return this.name
}

var obj1 = new Obj('huahua')
console.log(obj1)
console.log(obj1.getName())
obj1.setName('liul')
console.log(obj1.getName())


var obj2 = newF(Obj, 'huahua2')
console.log(obj2)
console.log(obj2.getName())
obj2.setName('liul2')
console.log(obj2.getName())

带返回值的情况:

function Obj(name) {
  this.name = name

  return {
    name: 'mod'
  }
}

var obj2 = newF(Obj, 'huahua2')
console.log(obj2) // { name: 'mod' }
console.log(obj2.getName()) // TypeError: obj2.getName is not a function
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容