1
0
Fork 0

[#25] Refactored common getter/setter code into common method within MemberBuilder

closure/master
Mike Gerwitz 2011-10-29 07:47:15 -04:00
parent 8433511f56
commit 2ba8e2c8f0
1 changed files with 43 additions and 23 deletions

View File

@ -207,22 +207,9 @@ exports.buildProp = function( members, meta, name, value, keywords, base )
*/ */
exports.buildGetter = function( members, meta, name, value, keywords, base ) exports.buildGetter = function( members, meta, name, value, keywords, base )
{ {
var prev_data = scanMembers( members, name, base );
this._validate.validateGetterSetter( this._buildGetterSetter(
name, keywords, prev_data 'get', members, meta, name, value, keywords, base
);
Object.defineProperty(
getMemberVisibility( members, keywords, name ),
name,
{
get: value,
enumerable: true,
// otherwise we can't add a setter to this
configurable: true,
}
); );
}; };
@ -246,6 +233,36 @@ exports.buildGetter = function( members, meta, name, value, keywords, base )
* @return {undefined} * @return {undefined}
*/ */
exports.buildSetter = function( members, meta, name, value, keywords, base ) exports.buildSetter = function( members, meta, name, value, keywords, base )
{
this._buildGetterSetter(
'set', members, meta, name, value, keywords, base
);
};
/**
* Copies a getter/setter to the appropriate member prototype, depending on
* visibility, and assigns necessary metadata from keywords
*
* XXX: Combine getter/setter operations for performance benefit
*
* @param {string} type get/set
*
* @param {{public: Object, protected: Object, private: Object}} members
*
* @param {Object} meta metadata container
* @param {string} name getter name
* @param {*} value getter value
*
* @param {Object.<string,boolean>} keywords parsed keywords
*
* @param {Object=} base optional base object to scan
*
* @return {undefined}
*/
exports._buildGetterSetter = function(
type, members, meta, name, value, keywords, base
)
{ {
var prev_data = scanMembers( members, name, base ); var prev_data = scanMembers( members, name, base );
@ -253,18 +270,21 @@ exports.buildSetter = function( members, meta, name, value, keywords, base )
name, keywords, prev_data name, keywords, prev_data
); );
this._validate.validateGetterSetter( name, members, keywords, base ); // the actual data to be set
var data = {
enumerable: true,
// otherwise we can't add a getter to this
configurable: true,
};
// set getter/setter appropriately, depending on the provided type
data[ type ] = value;
Object.defineProperty( Object.defineProperty(
getMemberVisibility( members, keywords, name ), getMemberVisibility( members, keywords, name ),
name, name,
{ data
set: value,
enumerable: true,
// otherwise we can't add a getter to this
configurable: true,
}
); );
}; };