new源碼實現

實現如下:

function Func(){}
let func = new Func()

function New (fn) {
  let obj = new Object
  obj.__proto__ = fn.prototype
  let result = fn.call(obj)
  if ( typeof(result) === 'object' ) {
    return result
  } else {
    return obj
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章