Skip to content

Commit

Permalink
Remove "reserved words" variable names (for IE6-8)
Browse files Browse the repository at this point in the history
Apparently `defineProperty` and `getOwnPropertyNames` cannot be used as
variable names or IE chokes with an “Object doesn’t support this
action” error.

See:
http://rameenak.wordpress.com/2010/04/26/object-doesnt-support-this-acti
on-error-in-ie7ie8/
  • Loading branch information
feross authored and James Halliday committed Dec 11, 2013
1 parent e1befc2 commit 60869cc
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ var LN2 = Math.LN2,

// ES5: lock down object properties
function configureProperties(obj) {
if (getOwnPropertyNames && defineProperty) {
var props = getOwnPropertyNames(obj), i;
if (getOwnPropNames && defineProp) {
var props = getOwnPropNames(obj), i;
for (i = 0; i < props.length; i += 1) {
defineProperty(obj, props[i], {
defineProp(obj, props[i], {
value: obj[props[i]],
writable: false,
enumerable: false,
Expand All @@ -49,15 +49,19 @@ function configureProperties(obj) {
// http://blogs.msdn.com/b/ie/archive/2010/09/07/transitioning-existing-code-to-the-es5-getter-setter-apis.aspx
// (second clause tests for Object.defineProperty() in IE<9 that only supports extending DOM prototypes, but
// note that IE<9 does not support __defineGetter__ or __defineSetter__ so it just renders the method harmless)
var defineProperty = Object.defineProperty || function(o, p, desc) {
if (!o === Object(o)) throw new TypeError("Object.defineProperty called on non-object");
if (ECMAScript.HasProperty(desc, 'get') && Object.prototype.__defineGetter__) { Object.prototype.__defineGetter__.call(o, p, desc.get); }
if (ECMAScript.HasProperty(desc, 'set') && Object.prototype.__defineSetter__) { Object.prototype.__defineSetter__.call(o, p, desc.set); }
if (ECMAScript.HasProperty(desc, 'value')) { o[p] = desc.value; }
return o;
};
var defineProp
if (!Object.defineProperty ||
!(function() { try { Object.defineProperty({}, 'x', {}); return true; } catch (e) { return false; } }())) {
defineProp = function(o, p, desc) {
if (!o === Object(o)) throw new TypeError("Object.defineProperty called on non-object");
if (ECMAScript.HasProperty(desc, 'get') && Object.prototype.__defineGetter__) { Object.prototype.__defineGetter__.call(o, p, desc.get); }
if (ECMAScript.HasProperty(desc, 'set') && Object.prototype.__defineSetter__) { Object.prototype.__defineSetter__.call(o, p, desc.set); }
if (ECMAScript.HasProperty(desc, 'value')) { o[p] = desc.value; }
return o;
};
}

var getOwnPropertyNames = Object.getOwnPropertyNames || function getOwnPropertyNames(o) {
var getOwnPropNames = Object.getOwnPropertyNames || function (o) {
if (o !== Object(o)) throw new TypeError("Object.getOwnPropertyNames called on non-object");
var props = [], p;
for (p in o) {
Expand All @@ -71,12 +75,12 @@ var getOwnPropertyNames = Object.getOwnPropertyNames || function getOwnPropertyN
// ES5: Make obj[index] an alias for obj._getter(index)/obj._setter(index, value)
// for index in 0 ... obj.length
function makeArrayAccessors(obj) {
if (!defineProperty) { return; }
if (!defineProp) { return; }

if (obj.length > MAX_ARRAY_LENGTH) throw new RangeError("Array too large for polyfill");

function makeArrayAccessor(index) {
defineProperty(obj, index, {
defineProp(obj, index, {
'get': function() { return obj._getter(index); },
'set': function(v) { obj._setter(index, v); },
enumerable: true,
Expand Down

0 comments on commit 60869cc

Please sign in to comment.