diff --git a/lib/util.js b/lib/util.js index f8b7cc5..db7b5c3 100644 --- a/lib/util.js +++ b/lib/util.js @@ -80,6 +80,8 @@ exports.secureFallback = function( val ) } secure_fallback = !!val; + exports.defineSecureProp = getDefineSecureProp(); + return exports; }; @@ -96,32 +98,7 @@ exports.secureFallback = function( val ) * * @return {undefined} */ -exports.defineSecureProp = function( obj, prop, value ) -{ - 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; - } - } -} +exports.defineSecureProp = getDefineSecureProp(); /** @@ -479,3 +456,47 @@ exports.arrayShrink = function( items ) 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 ); + } + }; + } +} +