JavaScript的表达式解析器-3. 成员属性系统

来源:互联网 发布:ntp 端口 编辑:程序博客网 时间:2024/05/16 10:18

为了方便实现成员的属性,使用了以下方法。该方法能够一次性生成属性的内部存储和Setter/Getter方法,但缺点是会浪费额外的空间。

var igame;importNamespace( 'igame.Base' );/*! Initialize global property count which is used as the surfix of properties'* name internally.*/( function (){if ( igame.GLOBAL_PROP_COUNT == undefined ){igame.GLOBAL_PROP_COUNT = 0;}} )();/*! Declare new property \param obj object which will be assigned a new property.\param name name of property.\param defaultValue default value of property.\param isReadonly indicates whether the property is read only.\param nameOfValidateFunc the function to validate assigned value.\param nameofConvertFunc the function to convert assigned value.*/igame.newProperty = function ( obj, name, defaultValue, isReadonly, nameOfValidateFunc, nameofConvertFunc ){//var value = defaultValue;var varName = '_private_' + obj.classname + '_' + name + '$' + igame.GLOBAL_PROP_COUNT++;obj[varName] = defaultValue;// Add getter    obj['get' + name] = function ()    {    //return value;    return obj[varName];    }// Add setter    if ( isReadonly )    {    obj['set' + name] = function ()    {    throw new Error( 'Property \'' + name + '\' is isReadonly' );    };    }    else    {    obj['set' + name] = function ( val )    {    if ( typeof obj[nameOfValidateFunc] == 'function' && !obj[nameOfValidateFunc]( val ) )    throw new Error( 'set' + name + ': invalid value' );        if ( typeof obj[nameofConvertFunc] == 'function' )    obj[varName] = obj[nameofConvertFunc]( val );    else    obj[varName] = val;    }    }} // function newProperty

一个使用该方法的例子是:

/*! OperatorBase */igame.Expression.Operator.OperatorBase = function (){igame.Base.call( this );igame.newProperty( this, 'Token', '' );}

...this.setToken('+');...log('Token is ' + this.getToken());...



原创粉丝点击