构造函数属性值应该是什么 .a 原型构造函数或对象构造函数本身
What should be the constructor properties value ..a prototypes constructor or the objects constructor itself
问题:为什么p1的构造函数出来是Person.不应该是Man ?
function Person()
{
this.type='person'
}
function Man()
{
this.type='Man'
}
Man.prototype=new Person();
var p1=new Man();
console.log('p1''s constructor is:'+p1.constructor);
console.log('p1 is instance of Man:'+(p1 instanceof Man));
console.log('p1 is instance of Person:'+(p1 instanceof Person));
http://jsfiddle.net/GSXVX/
覆盖原始prototype
对象时,您销毁了原始.constructor
属性...
Man.prototype = new Person();
您可以手动替换它,但它将是一个可枚举的属性,除非您使用非可枚举属性(在 ES5 实现中)。
Man.prototype=new Person();
Man.prototype.constructor = Man; // This will be enumerable.
// ES5 lets you make it non-enumerable
Man.prototype=new Person();
Object.defineProperty(Man.prototype, 'constructor', {
value: Man,
enumerable: false,
configurable: true,
writable: true
});
相关文章:
- javascript中对象构造函数中的var属性与this.properties
- 文本表示法VS.构造函数,用于在JavaScript中创建对象
- 如何使用此从对象访问构造函数
- 如何使用构造函数's的输出,以便将值插入到对象中
- 带有内置图像对象的Canvas组件构造函数;t显示's图像
- 未能运行构造函数:TypeError:对象#<对象>没有方法'addPlugin'
- JavaScript模块模式-如何在使用对象/函数之前激发构造函数/init函数
- jQuery双对象构造函数-例如$($(this))
- Javascript - 如何向对象构造函数添加属性
- 尝试将变量传递到对象中时的作用域问题'的方法构造函数
- 为什么实例构造函数指向最上面的函数构造函数对象
- 构造函数对象
- 如何使用exports或module.exports将函数构造函数对象方法与原型封装在单个模块中
- 如何添加构造函数对象与用户输入
- 如何将构造函数对象的值设置为其他值的函数
- 将构造函数对象传入自调用函数
- 构造函数对象名称- javascript
- Hook构造函数对象
- 为什么我的 HTML 构造函数对象返回 [object Object] 而不是 [HTMLElementElement]
- 从我的构造函数对象创建一个youtube iframe视频,这样我就可以创建多个视频