1
0
Fork 0

Using visibility fallback for browsers that do not support getters/setters

closure/master
Mike Gerwitz 2011-03-06 22:43:14 -05:00
parent df2943b5a1
commit c1765cd720
3 changed files with 38 additions and 4 deletions

View File

@ -788,6 +788,8 @@ function attachPropInit( prototype, properties, members )
this, class_instance[ this.__iid ], properties[ 'public' ] this, class_instance[ this.__iid ], properties[ 'public' ]
); );
class_instance[ this.__iid ] = inst_props;
propobj.setup( this, inst_props, properties, members ); propobj.setup( this, inst_props, properties, members );
}); });
} }

View File

@ -22,7 +22,12 @@
* @package core * @package core
*/ */
var util = require( './util' ); var util = require( './util' ),
// whether or not we support defining properties through
// Object.defineProperty()
defprop = ( Object.defineProperty !== undefined ) ? true : false;
;
exports.setup = function( base, dest, properties, members ) exports.setup = function( base, dest, properties, members )
@ -81,6 +86,11 @@ exports.createPropProxy = function( base, dest, props )
{ {
var hasOwn = Object.prototype.hasOwnProperty; var hasOwn = Object.prototype.hasOwnProperty;
if ( !defprop )
{
return base;
}
for ( prop in props ) for ( prop in props )
{ {
if ( !( hasOwn.call( props, prop ) ) ) if ( !( hasOwn.call( props, prop ) ) )
@ -114,3 +124,17 @@ exports.createPropProxy = function( base, dest, props )
return dest; return dest;
}; };
/**
* Returns whether property proxying is supported
*
* Proxying is done via getters and setters. If the JS engine doesn't support
* them (pre-ES5), then the proxy will not work.
*
* @return {boolean} true if supported, otherwise false
*/
exports.supportsPropProxy = function()
{
return defprop;
};

View File

@ -25,6 +25,7 @@
var common = require( './common' ), var common = require( './common' ),
assert = require( 'assert' ), assert = require( 'assert' ),
Class = common.require( 'class' ), Class = common.require( 'class' ),
propobj = common.require( 'propobj' ),
pub = 'foo', pub = 'foo',
prot = 'bar', prot = 'bar',
@ -145,6 +146,13 @@ var common = require( './common' ),
( function testProtectedAndPrivateMembersAreNotAccessibleExternally() ( function testProtectedAndPrivateMembersAreNotAccessibleExternally()
{ {
// browsers that do not support the property proxy will not support
// encapsulating properties
if ( !( propobj.supportsPropProxy() ) )
{
return;
}
assert.equal( assert.equal(
foo.peeps, foo.peeps,
undefined, undefined,