JS中(constructor属性)

  |  

在Javascript 语言中,constructor 属性是专门为 function 而设计的,它存在于每一个function 的prototype 属性中。这个constructor 保存了指向 function 的一个引用。

在定义一个函数(代码如下所示)时,

1
2
3
4
5
6
7
8
9
10
functionF() 

{

// some code

}

var f = new F();
console(f.constructor === F);// output true

JavaScript 内部会执行如下几个动作:1

1.为该函数添加一个原形(即 prototype)属性
2. 为 prototype 对象额外添加一个 constructor 属性,并且该属性保存指向函数F 的一个引用

1
2
3
4
5
6
7
8
// 继承
var f = new F();

class Api extends f.constructor {

}

export default new Api()
文章目录