diff --git a/CHANGELOG b/CHANGELOG index 31a86cb..326ba74 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Avoid object creation and an unnecessary function call in `Class#addMethods`, when working around JScript DontEnum bug. Replace with feature test and a simple boolean check at runtime. (kangax) + * Optimize Element#immediateDescendants. (kangax, Tobie Langel) * Remove unnecessary function object creation and `Number#times` in `Element._getContentFromAnonymousElement`. (kangax) diff --git a/src/lang/class.js b/src/lang/class.js index 3f59bb6..b277e2b 100644 --- a/src/lang/class.js +++ b/src/lang/class.js @@ -9,6 +9,17 @@ * inheritance](http://prototypejs.org/learn/class-inheritance). **/ var Class = (function() { + + // Some versions of JScript fail to enumerate over properties, names of which + // correspond to non-enumerable properties in the prototype chain + var IS_DONTENUM_BUGGY = (function(){ + for (var p in { toString: 1 }) { + // check actual property name, so that it works with augmented Object.prototype + if (p === 'toString') return false; + } + return true; + })(); + /** * Class.create([superclass][, methods...]) -> Class * - superclass (Class): The optional superclass to inherit methods from. @@ -138,9 +149,10 @@ var Class = (function() { var ancestor = this.superclass && this.superclass.prototype, properties = Object.keys(source); - // IE6 doesn't enumerate toString and valueOf properties, - // Force copy if they're not coming from Object.prototype. - if (!Object.keys({ toString: true }).length) { + // IE6 doesn't enumerate `toString` and `valueOf` (among other built-in `Object.prototype`) properties, + // Force copy if they're not Object.prototype ones. + // Do not copy other Object.prototype.* for performance reasons + if (IS_DONTENUM_BUGGY) { if (source.toString != Object.prototype.toString) properties.push("toString"); if (source.valueOf != Object.prototype.valueOf)