And here is a usecase: https://github.com/xavierm02/XJSAccessors/blob/master/XJSSelection.js
I'm planning on building a whole librairy on those accessors so if you have something to say, say it now :o ( I know the way XJSSelection is made sucks because it tests the same thing each time you call it whereas it should only test one and then assign the function. But it is just a usecase. And I just found out I wanted the storage to be inherited from the DOMSelection, not the XJSSelection, si I'll add this possibility to XJSAccessors.) On Sat, Aug 27, 2011 at 1:43 AM, Xavier MONTILLET <[email protected]> wrote: > Here is the code on Github: > https://github.com/xavierm02/XJSAccessors/blob/master/XJSAccessors.js > > On Sat, Aug 27, 2011 at 1:00 AM, Xavier MONTILLET > <[email protected]> wrote: >> As the aim is to prevent external code from messing with these >> variables, I'll even use hasOwnProperty. >> >> On Sat, Aug 27, 2011 at 12:03 AM, Poetro <[email protected]> wrote: >>> 2011/8/26 Xavier MONTILLET <[email protected]>: >>>> Here is how protected variables would work: >>>> >>>> ( function ( ) { >>>> >>>> function XJSAccessors( object ) { >>>> var undefined; >>>> this.object = object; >>>> var storage = this.storage = Object.create( object ); >>>> var getters = this.getters = { }; >>>> var setters = this.setters = { }; >>>> >>>> object.get = function ( name ) { >>>> var getter = getters[ name ]; >>>> return getter === undefined ? storage[ name ] : getter( >>>> name, object ); >>>> }; >>>> object.set = function ( name, value ) { >>>> var setter = setters[ name ]; >>>> return storage[ name ] = setter === undefined ? value : >>>> setter( value, name, object ); >>>> }; >>>> object.forIn = function( callback, that ) { >>>> if ( argument.length < 2 ) { >>>> that = this; >>>> } >>>> for ( var name in this ) { >>>> callback.call( that, this.get( name ), name, this ); >>>> } >>>> }; >>>> } >>>> >>>> var XJSAccessorsPrototype = XJSAccessors.prototype; >>>> function protectedSetter( value, name, object ) { >>>> return object.get( name ); >>>> } >>>> XJSAccessorsPrototype.protect = function ( name ) { >>>> this.setters[ name ] = protectedSetter; >>>> }; >>>> >>>> this.XJSAccessors = XJSAccessors; >>>> } )( ); >>>> >>>> var o = { }; >>>> var a = new XJSAccessors( o ); >>>> console.log( o.set( 'p', 'v' ) );// 'v' >>>> a.protect( 'p' ); >>>> console.log( o.set( 'p', 'WHATEVER' ) );;// 'v' >>> >>> Why checking for undefined, why not check if it is in the object: >>> return name in getters ? getter( name, object ) : storage[ name ]; >>> >>> and >>> >>> return name in setters ? setter( value, name, object ) : storage[ name >>> ] = value; >>> >>> -- >>> Poetro >>> >>> -- >>> To view archived discussions from the original JSMentors Mailman list: >>> http://www.mail-archive.com/[email protected]/ >>> >>> To search via a non-Google archive, visit here: >>> http://www.mail-archive.com/[email protected]/ >>> >>> To unsubscribe from this group, send email to >>> [email protected] >>> >> > -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
