From ad4b3179552354f918b88ca3166249e2c4eb8b03 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Sun, 14 Nov 2010 01:37:12 -0500 Subject: [PATCH] Refactored defineProperty() code into define_secure_prop() function to reduce duplicate code --- lib/class.js | 64 ++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/lib/class.js b/lib/class.js index 1dca210..4528d6d 100644 --- a/lib/class.js +++ b/lib/class.js @@ -226,6 +226,38 @@ function setup_props( func, class_data ) } +/** + * Attempts to define a non-enumerable, non-writable and non-configurable + * property on the given object + * + * If the operation is unsupported, a normal property will be set. + * + * @param {Object} obj object to set property on + * @param {string} prop name of property to set + * @param {mixed} value value to set + * + * @return {undefined} + */ +function define_secure_prop( obj, prop, value ) +{ + if ( Object.defineProperty === undefined ) + { + func[ prop ] = value; + } + else + { + Object.defineProperty( obj, prop, + { + value: value, + + enumerable: false, + writable: false, + configurable: false, + }); + } +} + + /** * Attaches isAbstract() method to the class * @@ -251,21 +283,7 @@ function attach_abstract( func, methods ) // attach the list of abstract methods to the class - if ( Object.defineProperty === undefined ) - { - func.abstractMethods = methods; - } - else - { - Object.defineProperty( func, 'abstractMethods', - { - value: methods, - - enumerable: false, - writable: false, - configurable: false, - }); - } + define_secure_prop( func, 'abstractMethods', methods ); } @@ -295,19 +313,5 @@ function attach_extend( func ) // if defineProperty is unsupported, do it the old fashioned way (it's just // less restrictive) - if ( Object.defineProperty === undefined ) - { - func.extend = ext_method; - } - else - { - Object.defineProperty( func, 'extend', - { - value: ext_method, - - enumerable: false, - writable: false, - configurable: false, - } ); - } + define_secure_prop( func, 'extend', ext_method ); }