Golmaal
Golmaal

Reputation: 669

Accessor Descriptor: How to use 'get' and 'set' in practice?

I am not sure if I am getting it right.

This example is straight from MDN (Mozilla Developer Network):

var bValue;  
Object.defineProperty(o, "b", {get : function(){ return bValue; },  
                               set : function(newValue){ bValue = newValue; },  
                               enumerable : true,  
                               configurable : true});  

What happens is - it creates a global variable named bValue, which is not done. I understand that this example only demonstrates the use and thus it is okay if it creates a global variable. But if I am going to use this in an application, I will modify it slightly, by adding the this keyword:

Object.defineProperty(o, "b", {get : function(){ return this.bValue; },  
                               set : function(newValue){ this.bValue = newValue; },  
                               enumerable : true,  
                               configurable : true}); 

Now, the object o will have property b, and at the same time, it will also have another property bValue. The user (programmer) will be exposed only to 'b' and not to 'bValue' though he can still access bValue directly - I don't see how it can be prevented.

I understand that the property b and the property bValue may not always be same, but b would depend on the value of bValue because the getter and setter allow us to pre-process bValue before assigning the value to b.

The main question is, am I getting it right? Or am I missing something here?

Upvotes: 4

Views: 2668

Answers (2)

pimvdb
pimvdb

Reputation: 154818

You seem to be looking for a closure. This is a coding pattern that enables you to use private variables and only expose what you want to expose (public variables).

(function() {
    var bValue;

    Object.defineProperty(o, "b", {get : function(){ return bValue; },  
                                   set : function(newValue){ bValue = newValue; },  
                                   enumerable : true,  
                                   configurable : true}); 
})();

It creates a function and executes it immediately. This seems useless, but since functions introduce a level of scoping, bValue is not accessible anywhere but within the function this way.

The o.b property acts as a delegate between the developer and the value. You cannot access bValue itself. (Though in this example the getter/setter obviously act such that they exactly do the same as using bValue directly.)

http://jsfiddle.net/W4CSE/2/

Upvotes: 7

Martin Jespersen
Martin Jespersen

Reputation: 26183

The idea is to wrap the code in a closure, thus hiding bValue from the world, like this:

var o =(function() {

    var o={},bValue;    

   Object.defineProperty(o, "b", {get : function(){ return bValue; },  
                               set : function(newValue){ bValue = newValue; },  
                               enumerable : true,  
                               configurable : true});


    return o;

})();

now you can reference o.b but not bValue

Upvotes: 3

Related Questions