1.new一个函数的时候发生了什么


new一个构造函数本质就是实例化一个对象


1.新建一个对象
2.constructor.call(新建的对象)
3.把函数的prototype属性赋值给新建的对象的__proto__

 

2.初始化一个函数的时候发生了什么?

function a() { }
声明了一个为a的函数


JavaScript解释器构造函数对象a
在a内部初始化一个属性prototype


prototype指向一个原型对象,

{

  constructor://a本身

  __proto__:Object

}


3. 如何在JavaScript中实现继承

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

let father = new father()

function Son(name){
father.call(this,name)
}

Son.prototype = Object.create(father.prototype)

son.prototype.constructor = son

 

// new Son('儿子')

 

 

相关文章:

  • 2021-08-17
  • 2021-05-28
  • 2022-02-12
  • 2021-11-11
  • 2021-09-02
  • 2021-06-06
  • 2022-01-21
猜你喜欢
  • 2022-12-23
  • 2021-06-03
  • 2022-02-19
  • 2021-12-19
  • 2021-12-08
相关资源
相似解决方案