From c1765cd7203090f151ace1bc4b24501844fd97cb Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Sun, 6 Mar 2011 22:43:14 -0500 Subject: [PATCH] Using visibility fallback for browsers that do not support getters/setters --- lib/class.js | 2 ++ lib/propobj.js | 26 +++++++++++++++++++++++++- test/test-class-visibility.js | 14 +++++++++++--- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/lib/class.js b/lib/class.js index 36778f3..1ba3f20 100644 --- a/lib/class.js +++ b/lib/class.js @@ -788,6 +788,8 @@ function attachPropInit( prototype, properties, members ) this, class_instance[ this.__iid ], properties[ 'public' ] ); + class_instance[ this.__iid ] = inst_props; + propobj.setup( this, inst_props, properties, members ); }); } diff --git a/lib/propobj.js b/lib/propobj.js index 8a1b744..dba5208 100644 --- a/lib/propobj.js +++ b/lib/propobj.js @@ -22,7 +22,12 @@ * @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 ) @@ -81,6 +86,11 @@ exports.createPropProxy = function( base, dest, props ) { var hasOwn = Object.prototype.hasOwnProperty; + if ( !defprop ) + { + return base; + } + for ( prop in props ) { if ( !( hasOwn.call( props, prop ) ) ) @@ -114,3 +124,17 @@ exports.createPropProxy = function( base, dest, props ) 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; +}; + diff --git a/test/test-class-visibility.js b/test/test-class-visibility.js index 726a0da..b32fb7f 100644 --- a/test/test-class-visibility.js +++ b/test/test-class-visibility.js @@ -22,9 +22,10 @@ * @package test */ -var common = require( './common' ), - assert = require( 'assert' ), - Class = common.require( 'class' ), +var common = require( './common' ), + assert = require( 'assert' ), + Class = common.require( 'class' ), + propobj = common.require( 'propobj' ), pub = 'foo', prot = 'bar', @@ -145,6 +146,13 @@ var common = require( './common' ), ( function testProtectedAndPrivateMembersAreNotAccessibleExternally() { + // browsers that do not support the property proxy will not support + // encapsulating properties + if ( !( propobj.supportsPropProxy() ) ) + { + return; + } + assert.equal( foo.peeps, undefined,