js new的时候干了些啥?

  • 创建一个空的简单JavaScript对象(即{})
  • 链接该对象(即设置该对象的构造函数)到另一个对象
  • 将步骤1新创建的对象作为this的上下文
  • 如果该函数没有返回对象,则返回this

模仿new

let that = this
    function CutOb () {
      this.date = that.getNowDate()
    }
    let cutOb = new CutOb()
    function createObject (fn) {
      let object = {}
      Object.setPrototypeOf(object, fn.prototype)
      fn.call(object)
      return object
    }
    let cut = createObject(CutOb)
    console.log(cutOb, cut)

猜你喜欢

转载自blog.csdn.net/qq_27449993/article/details/123002995