在构造函数函数中分配原型方法:电位陷阱
函数缩写: 是多次分配的,多次分配,这是不必要的,并且可能具有不必要的性能含义。原型方法内的构造函数可能会导致意外结果。例如:
In this scenario, get() returns the value of c2's local variable value instead of c1's because the method closure references the most recently defined value on the prototype.
var Filter = function( category, value ){ this.category = category; this.value = value; // product is a JSON object Filter.prototype.checkProduct = function( product ){ // run some checks return is_match; } };Other Considerations:
var Filter = function( category, value ){ this.category = category; this.value = value; // product is a JSON object Filter.prototype.checkProduct = function( product ){ // run some checks return is_match; } };
Prototyping Outside the构造函数:第一个结构禁止访问构造函数之外的原型,潜在地限制灵活性。方法放置:
在第二个结构中,方法直接放置在对象上,而不是原型,这可能会在某些原型上提高性能case。免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3