1
0
Fork 0

Added getter/setter override support

closure/master
Mike Gerwitz 2010-11-10 23:19:46 -05:00
parent 0c963d1d00
commit 8422bea1d6
2 changed files with 123 additions and 2 deletions

View File

@ -51,8 +51,25 @@ var prop_copy = function( props, dest )
// if the property already exists, then it's being overridden (we only
// care about methods - properties will simply have their values
// overwritten)
var pre = dest[ property ];
if ( ( pre !== undefined ) && ( pre instanceof Function ) )
var pre = dest[ property ],
getter = props.__lookupGetter__( property ),
setter = props.__lookupSetter__( property );
// check for getter/setter overrides
if ( getter || setter )
{
if ( getter )
{
dest.__defineGetter__( property, getter );
}
if ( setter )
{
dest.__defineSetter__( property, setter );
}
}
// check for method overrides
else if ( ( pre !== undefined ) && ( pre instanceof Function ) )
{
dest[ property ] = ( function( method, super_method )
{

View File

@ -0,0 +1,104 @@
/**
* Tests class getter/setter inheritance
*
* 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 test
*/
require( './common' );
var assert = require( 'assert' ),
Class = require( 'class' );
var Foo = Class.extend(
{
_foo: '',
set foo( val )
{
this._foo = ''+( val );
},
get foo()
{
return this._foo;
},
set bar( val )
{
},
get bar()
{
return 'durp';
},
});
var SubFoo = Foo.extend(
{
bar2: null,
set bar( val )
{
this.bar2 = val;
},
get bar()
{
return this.bar2;
},
});
var foo = new Foo(),
sub = new SubFoo(),
val = 'val';
// ensure we have our act together before continuing
foo.foo = val;
assert.equal(
foo.foo,
val,
"Sanity check"
);
// foo should be inherited as-is (if this doesn't work, someone went out of
// their way to break it, as it works by default!)
sub.foo = val = 'val2';
assert.equal(
sub.foo,
val,
"Subtypes should inherit getter/setters"
);
sub.bar = val = 'val3';
assert.equal(
sub.bar,
val,
"Subtypes should be able to override getter/setters"
);
assert.equal(
sub.bar2,
val,
"Subtypes should be able to override getter/setters (check 2)"
);