ES5 has a enumerable flag. Example
Example
var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor
, pd = getOwnPropertyDescriptor(Object.prototype, "toString");
assert(pd.enumerable === false, "enumerability has the wrong value");
Partial implementation
Partial implementation is do-able by having Object.keys
and Object.getOwnPropertyNames
filter out new non-enumerable properties using the shimmed Object.defineProperty
.
Introduction
This allows for properties to be non enumerable. This clearly means that Example
for (var key in {}) {
assert(key !== "toString", "I should never print");
}
This allows us to add properties to say Object.prototype
(Example)
Object.defineProperty(Object.prototype, "toUpperCaseString", {
value: function toUpperCaseString() {
return this.toString().toUpperCase();
},
enumerable: false
});
for (var key in {}) {
assert(key !== "toUpperCaseString", "I should never print");
}
console.log(({}).toUpperCaseString()); // "[OBJECT OBJECT]"
Question
How can we emulate this in non-ES5 compliant browsers?
Browser compat table
In this case we care about potentially solving this for
- IE < 9 (IE8 only works on DOM objects)
- Firefox 3.6
- Safari 4
- Opera 11.5 (Opera 11.6 solves this).
The ES5-shim does not have a solution for this.
The ES5 shim has a solution for most ES5 features that will break your code if it doesn't work.
Is there any black magic that can be done with propiotory IE only APIs? Maybe with VBScript?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…