乐趣区

关于javascript:实现-new-方法

// 创立一个空对象,链接到原型,绑定 this 值,返回新对象
function createNew(){let obj = {}
 let constructor = [].shift.call(arguments)
 obj.__proto__ = constructor.prototype
 let result = constructor.apply(obj,arguments)
 return typeof result==='object'?result:obj
}
function People(name,age){
  this.name = name
  this.age=age
}
let peo = createNew(People,'Allan',22)
console.log(peo.name)
console.log(peo.age)
退出移动版