【发布时间】:2011-06-24 01:50:26
【问题描述】:
在JavaScript Prototype继承中,添加prototype.constructor属性的目的是什么。让我用一个例子来解释。
变种超级=函数(){ this.superProperty = '超级属性' } 变子 = 函数() { this.subProperty = '子属性' } Sub.prototype = new Super(); Sub.prototype.constructor = Sub; // 语句的优点 var inst = new Sub();无论是否添加 Sub.prototype.constructor = Sub,以下行在所有情况下都返回 true。
console.log(inst instanceof Sub) // true console.log(inst instanceof Super) // true我猜,它在获取新实例时可能很有用,但何时和/或如何?
提前致谢。
【问题讨论】:
标签: javascript inheritance prototype constructor