Search code examples
javascriptmetaprogramming

JavaScript automatic getter/setters (John Resig book)


I'm reading "Pro JavaScript Techniques" by John Resig, and I'm confused with an example. This is the code:

// Create a new user object that accepts an object of properties
function User( properties ) {
  // Iterate through the properties of the object, and make sure
  // that it's properly scoped (as discussed previously)
  for ( var i in properties ) { (function(){
  // Create a new getter for the property
  this[ "get" + i ] = function() {
    return properties[i];
  };
  // Create a new setter for the property
  this[ "set" + i ] = function(val) {
    properties[i] = val;
  };
})(); }
}

// Create a new user object instance and pass in an object of
// properties to seed it with
var user = new User({
  name: "Bob",
  age: 44
});

// Just note that the name property does not exist, as it's private
// within the properties object
alert( user.name == null );

// However, we're able to access its value using the new getname()
// method, that was dynamically generated
alert( user.getname() == "Bob" );

// Finally, we can see that it's possible to set and get the age using
// the newly generated functions
user.setage( 22 );
alert( user.getage() == 22 );

Now running that in the Firebug console (on Firefox 3) throws that user.getname() is not a function. I tried doing this:

var other = User
other()
window.getname() // --> This works!

And it worked!

Why?

Doing:

var me = this;

seems to work a bit better, but when executing "getname()" it returns '44' (the second property)...

Also I find it strange that it worked on the window object without modification...

And a third question, what's the difference between PEZ solution and the original? (He doesn't use an anonymous function.)


Solution

  • I started this post with the sole purpose of learning why that things happened, and I finally did. So in case there's someone else interested in the "whys", here they are:

    Why does 'this' changes inside the anonymous function?

    A new function, even if it is an anonymous, declared inside an object or another function, always changes the scope, in this case returning to the global scope (window).

    Solution: all stated in the post, I think the clearer is executing the anonymous function with .call(this).

    Why does getname() always return the age?

    While the anonymous function gets executed right away, the getters/setters get executed for the first time when they are called. In that moment, the value of i will always be the last, because it has already iterated for all the properties... and it will always return properties[i] which is the last value, in this case the age.

    Solution: save the i value in a variable like this

     for ( i in properties ) { (function(){ 
      var j = i
      // From now on, use properties[j]