1
0
Fork 0

util.defineSecureProp() no longer performs fallback check on each invocation

closure/master
Mike Gerwitz 2010-12-21 22:41:50 -05:00
parent d5f37f294e
commit 7e7080ccbf
1 changed files with 47 additions and 26 deletions

View File

@ -80,6 +80,8 @@ exports.secureFallback = function( val )
} }
secure_fallback = !!val; secure_fallback = !!val;
exports.defineSecureProp = getDefineSecureProp();
return exports; return exports;
}; };
@ -96,32 +98,7 @@ exports.secureFallback = function( val )
* *
* @return {undefined} * @return {undefined}
*/ */
exports.defineSecureProp = function( obj, prop, value ) exports.defineSecureProp = getDefineSecureProp();
{
if ( secure_fallback )
{
obj[ prop ] = value;
}
else
{
try
{
Object.defineProperty( obj, prop,
{
value: value,
enumerable: false,
writable: false,
configurable: false,
});
}
catch ( e )
{
// if there's an error (ehem, IE8), fall back
obj[ prop ] = value;
}
}
}
/** /**
@ -479,3 +456,47 @@ exports.arrayShrink = function( items )
return arr_new; return arr_new;
} }
/**
* Appropriately returns defineSecureProp implementation to avoid check on each
* invocation
*
* @return {function( Object, string, * )}
*/
function getDefineSecureProp()
{
// falls back to simply defining a normal property
var fallback = function( obj, prop, value )
{
obj[ prop ] = value;
};
if ( secure_fallback )
{
return fallback;
}
else
{
// uses ECMAScript 5's Object.defineProperty() method
return function( obj, prop, value )
{
try
{
Object.defineProperty( obj, prop,
{
value: value,
enumerable: false,
writable: false,
configurable: false,
});
}
catch ( e )
{
// if there's an error (ehem, IE8), fall back
fallback( obj, prop, value );
}
};
}
}