function FunctionClass() {
this.name = "test";
}
FunctionClass.prototype = {
setVal: function() {
console.log("xxx");
}
}
console.log(FunctionClass.prototype.constructor === FunctionClass)
会有什么问题?
可否举几个例子,先谢过大家。
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
FunctionClass.prototype = {}你这样写本质上重写了默认的prototype对象,因此constructor属性也变成了新的对象的constructor属性(指向Object构造函数),不再指向FunctionClass函数。另外重设
constructor属性会导致它的[[Enumerable]]特性被设置为true。默认情况下,原生的constructor属性是不可枚举的,可以使用Object.defineProperty():---- 参见红宝书155页。
FunctionClass.prototype变成了Object
用 FunctionClass.prototype 这一条会重写原型链导致 constructor 丢失变为 Object。
建议这么写:
或者在重写prototype后,将 constructor 重置为 FunctionClass。