有哪些 constructor
在 javascript 中,涉及到 constructor 的概念有三个
- 构造函数可被称为一个 constructor
- 所有的对象有一个 constructor 属性
- ES6 语法中有一个 constructor 关键字
一脸懵逼…
构造函数 —— constructor
1 | function Person(name) { |
这就是 javascript 中的构造函数,又可称为一个构造器(constructor)
对象的 constructor 属性
Object.prototype.constructor 属性返回了一个引用,这个引用指向产生实例的构造函数
Returns a reference to the Object constructor function that created the instance object
1 | function Person(name) { |
1 | var o = new Object; |
constructor 属性会被覆盖
1 | function Person(name) { |
这就是最好不用 constructor 确定实例(instance)所属类型的原因
判断实例类型,用 instanceof 关键字