1
0
Fork 0

Began factoring property instance object out of class module

closure/master
Mike Gerwitz 2011-03-06 18:19:19 -05:00
parent 5d8fdc5204
commit e463d2c411
3 changed files with 86 additions and 52 deletions

View File

@ -23,7 +23,9 @@
*/ */
var util = require( './util' ), var util = require( './util' ),
member_builder = require( './member_builder' ); member_builder = require( './member_builder' ),
propobj = require( './propobj' )
;
/** /**
* Stores class metadata internally (ensures data is encapsulated) * Stores class metadata internally (ensures data is encapsulated)
@ -772,10 +774,6 @@ function initInstance( iid, instance )
*/ */
function attachPropInit( prototype, properties, members ) function attachPropInit( prototype, properties, members )
{ {
var prop_pub = properties[ 'public' ],
prop_prot = properties[ 'protected' ]
;
util.defineSecureProp( prototype, '__initProps', function() util.defineSecureProp( prototype, '__initProps', function()
{ {
var inst_props = class_instance[ this.__iid ]; var inst_props = class_instance[ this.__iid ];
@ -788,52 +786,7 @@ function attachPropInit( prototype, properties, members )
parent_init.call( this ); parent_init.call( this );
} }
// initialize each of the properties for this instance to propobj.setup( this, inst_props, properties, members );
// ensure we're not sharing prototype values
for ( prop in prop_pub )
{
// initialize the value with a clone to ensure that they do
// not share references (and therefore, data)
this[ prop ] = util.clone( prop_pub[ prop ] );
( function( prop )
{
var inst = this;
// public properties, when set internally, must forward to the
// actual variable
inst_props.__defineSetter__( prop, function( val )
{
inst[ prop ] = val;
} );
// since we're defining a setter, we'll need to define a getter
// to return the value, or we'll simply return undefined
inst_props.__defineGetter__( prop, function()
{
return inst[ prop ];
} );
} ).call( this, prop );
}
var methods_protected = members[ 'protected' ],
hasOwn = Array.prototype.hasOwnProperty
;
// copy over the methods
for ( method_name in methods_protected )
{
if ( hasOwn.call( methods_protected, method_name ) )
{
inst_props[ method_name ] = methods_protected[ method_name ];
}
}
// initialize protected properties and store in instance data
for ( prop in prop_prot )
{
inst_props[ prop ] = util.clone( prop_prot[ prop ] );
}
}); });
} }

81
lib/propobj.js 100644
View File

@ -0,0 +1,81 @@
/**
* Contains property object generator
*
* Copyright (C) 2010 Mike Gerwitz
*
* This file is part of ease.js.
*
* ease.js is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Mike Gerwitz
* @package core
*/
var util = require( './util' );
exports.setup = function( base, dest, properties, members )
{
var prop_pub = properties[ 'public' ],
prop_prot = properties[ 'protected' ]
;
// initialize each of the properties for this instance to
// ensure we're not sharing references to prototype values
for ( prop in prop_pub )
{
// initialize the value with a clone to ensure that they do
// not share references (and therefore, data)
base[ prop ] = util.clone( prop_pub[ prop ] );
( function( prop )
{
var inst = this;
// public properties, when set internally, must forward to the
// actual variable
dest.__defineSetter__( prop, function( val )
{
inst[ prop ] = val;
} );
// since we're defining a setter, we'll need to define a getter
// to return the value, or we'll simply return undefined
dest.__defineGetter__( prop, function()
{
return inst[ prop ];
} );
} ).call( base, prop );
}
var methods_protected = members[ 'protected' ],
hasOwn = Array.prototype.hasOwnProperty
;
// copy over the methods
for ( method_name in methods_protected )
{
if ( hasOwn.call( methods_protected, method_name ) )
{
dest[ method_name ] = methods_protected[ method_name ];
}
}
// initialize protected properties and store in instance data
for ( prop in prop_prot )
{
dest[ prop ] = util.clone( prop_prot[ prop ] );
}
}

View File

@ -28,7 +28,7 @@ TPL_VAR='/**{CONTENT}**/'
RMTRAIL="$PATH_TOOLS/rmtrail" RMTRAIL="$PATH_TOOLS/rmtrail"
# order matters # order matters
CAT_MODULES="prop_parser util member_builder class interface" CAT_MODULES="prop_parser util member_builder propobj class interface"
## ##
# Output template header # Output template header