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'

On Fri, Aug 26, 2011 at 11:36 PM, Xavier MONTILLET
<[email protected]> wrote:
> I was rather thinking of the case where you just put a get and a set
> methods. Those are the only 2 properties exposed and all the rest is
> "protected".
>
> On Fri, Aug 26, 2011 at 10:49 PM, Scott Sauyet <[email protected]> wrote:
>> xavierm02 wrote:
>>> I'm doing a lib of my own, mostly to learn more about JavaScript and one of
>>> the things I like is having a minimum of things available from outside my
>>> objects.
>>> Therefore, I use getters and setters.
>>
>> Right off the bat, I don't follow how this means having a minimum of
>> things available from outside an object.  If you have an object `foo`
>> with one property `bar`, how does using `foo.getBar` and `foo.setBar`
>> minimize your surface area?  If your setters and getters do nothing
>> more than setting and getting a property, you haven't encapsulated
>> private data, and you have actually increased the size of the
>> interface exposed by your object.
>>
>> As a learning exercise for the language, the techniques you discuss
>> might be useful.  But I'm not sure I really see the real-world
>> application of them.
>>
>>  -- Scott
>>
>> --
>> 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]

Reply via email to