Possible mistakes in Pro JavaScript Techniques

I’m previewing the the ultimate JavaScript book for the modern web developer and have found several, maybe, mistakes.

A side effect of the anonymous function scope induction trick

At the end of Chapter 2 >> Privileged Methods

Listing 2-25. Example of Dynamically Generated Methods That Are Created When a New Object Is Instantiated

// 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 );

The example code won’t work as expected. My test with Firefox 2.0.0.1 shows that the user.getname and user.getage are actually undefined. But window.getname and window.getage are there! The error is caused by the scope induction trick:
(function(){})(). Inside the anonymous function, the this variable somehow points to the window object! In the simplest case:

var o = {f: function() {(function(){alert(this === window);})();}}; o.f(); //alerts true (but false if you evaluate the whole line in Firebug 1.0b8)
Seems that the implementation treats anonymous functions as properties of the window object?
 

null, 0, ‘’, false, and undefined are NOT all equal (==) to each other

In Chapter 3 >> != and == vs. !== and ===

…In JavaScript, null, 0, ‘’, false, and undefined are all equal (==) to each other, since they all evaluate to false…

Listing 3-12. Examples of How != and == Differ from !== and ===
// Both of these are true
null == false
0 == undefined
// You should use !== or === instead
null !== false
false === false

Actually 0, ” and false all equal (==) to each other and null equals (==) to undefined but both null == false and undefined == false evaluate to false. This is reasonable because both null and undefined indicate "no value" while false is a valid value.

This entry was posted in JavaScript. Bookmark the permalink.

4 Responses to Possible mistakes in Pro JavaScript Techniques

  1. DBJ says:

     Why the scope induction trick? I made the code bellow , which is using IE7 engine … but it behaves stangely .. does anyone knows why ?
     
    // Create a new user object that accepts an object of propertiesfunction 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 ) {   // 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 withvar user1 = new User({ name: "Bob", age: 44 } );var user2 = new User({ name: "Axe", age: 94 } );
    // name returns age ? why … ?// within the properties objectuser2.get_name()/*94*/

  2. Arrix says:

    @ (no name) Why the scope induction trick? I
    made the code bellow , which is using IE7 engine … but it behaves
    stangely .. does anyone knows why ?Your dynamically created getter/setter functions are defined as nested function and forming closures. Each of these functions can "see" the local variable i (not the instant value of i). JavaScript is different from Ruby here. You need to make i evaluated in each loop, e.g. via function call.

  3. Unknown says:

    wow gold!All wow gold US Server 24.99$/1000G on sell! Cheap wow gold,wow gold,wow gold,Buy Cheapest/Safe/Fast WoW US EU wow gold Power leveling wow gold from the time you wWorld of Warcraft gold ordered!

    wow power leveling wow power leveling power leveling wow power leveling wow powerleveling wow power levelingcheap wow power leveling wow power leveling buy wow power leveling wow power leveling buy power leveling wow power leveling cheap power leveling wow power leveling wow power leveling wow power leveling wow powerleveling wow power leveling power leveling wow power leveling wow powerleveling wow power leveling buy rolex cheap rolex wow gold wow gold wow gold wow gold -210133792473397

Leave a comment