Archive for September, 2009

JavaScript Tidbit: Block scope with “let”

Tuesday, September 15th, 2009

JavaScript has functional scope. Meaning that if you (properly) define variables within functions, those variables remain accessible only inside the function.

Block scope, on the other hand, defines scope within a block of code, usually defined by braces. JavaScript now has block scope as of version 1.7, which means it’s available in these browsers:

  • Firefox 2+

Block scope is enabled in JavaScript with the use of “let”:

let(x=100) {
    alert(x);
};

It also works perfectly well on one line, without the use of braces:

let(x=100) alert(x);

Note that we can define global variables with the same name outside the block scope and the variables won’t interfere with each other:

x = 200;
let(x=100) alert(x);
alert(x);
// result: 100, 200

Note that there’s a slight caveat – not only is this not available in any version of IE, but it also requires a special script type declaration in order to work (at least for Firefox): type=”text/javascript;version=1.7″

References:
JavaScript Versions
Video: Best Practices in Javascript Library Design (John Resig)
New in JavaScript 1.7

PHP Tidbit: Dead simple singleton

Monday, September 14th, 2009
class Singleton {
    private static $instance;
    public static function getInstance() {
        if(!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c();
        }
        return self::$instance;
    }
}

And the explanation…

class Singleton {
    private static $instance;               // static variable to hold our 1 instance

    public static function getInstance() {  // function to get the 1 instance
        if(!isset(self::$instance)) {       // this will only run once (and instantiate once)
            $c = __CLASS__;                 // get the class (Singleton)
            self::$instance = new $c();     // instantiate the class and store it in our variable
        }
        return self::$instance;             // return the instance
    }

    public static function myFunction() {   // we can get to this through Singleton::getInstance()->myFunction()
        // ...
    }
}

JavaScript Tidbit: Shorthand Object Instantiation

Monday, September 14th, 2009

The skinny: here’s a neat little trick to reduce the amount of code users have to type to instantiate objects:

function jQuery(str, con){
    if ( window == this )
        return new jQuery(str, con);
    // ...
}

new jQuery("#foo"); // this is now equivalent...
jQuery("#foo");     // ...to this!

Explanation: when jQuery(“#foo”) is typed, the function first determines if the context in which it’s being called is the global object (window).  If it is, it returns an instantiation of itself.  When it’s instantiated, the context in which it’s being called is inside its own function, and NOT within the global object (window), thus it bypasses our little instantiation trick and moves on to execute any remaining code in the function.

From John Resig’s Best Practices in Javascript Library Design