[英]jQuery.extend and buggy IE non-enumerable props
I'd like to extend the prototype of my custom constructor function with $.extend
. 我想用$.extend
扩展自定义构造函数的原型。 The extender object contains a custom toString
method that will not be enumerable in IE (8?). 扩展程序对象包含一个自定义的toString
方法,该方法在IE(8?)中无法枚举。 I didn't find out whether jQuery fixes this problem internally or not. 我没有发现jQuery是否在内部解决了此问题。
var myConstructor = function() { /* ... */ };
$.extend(myConstructor.prototype, {
toString: function() { return "foo"; }
});
Will this work? 这样行吗? And if not: Is there a quick fix or do I need to use my own for-in loop? 如果没有,是否可以快速解决?还是需要使用自己的for-in循环?
I had the chance to test it myself and jQuery does not. 我有机会自己测试,而jQuery没有。 Here is a solution I found: 这是我找到的解决方案:
var extendPrototype = function() {
var objectPrototype = Object.prototype,
hasOwnProperty = objectPrototype.hasOwnProperty,
isBuggy = !{valueOf: 0}.propertyIsEnumerable("valueOf"),
keys;
if(isBuggy)
keys = "constructor,hasOwnProperty,isPrototypeOf,propertyIsEnumerable,toLocaleString,toString,valueOf".split(",");
return function(prototype, object) {
var i,
key;
for(key in object)
if(hasOwnProperty.call(object, key))
prototype[key] = object[key];
if(isBuggy) {
i = 0;
for(i; key = keys[i]; i ++)
if(object[key] !== objectPrototype[key] || hasOwnProperty.call(object, key))
prototype[key] = object[key]
}
return prototype
}
}();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.