הלינקייה: מגזין חודשי למפתחים

רוצה לשמוע על כל האירועים, המדריכים, הקורסים והמאמרים שנכתבו החודש ?
הלינקייה הינו מגזין חופשי בעברית שמשאיר אותך בעניינים.
בלי ספאם. בלי שטויות. פעם בחודש אצלך בתיבה.

Javascript Style Guide

/**
 * Each file must be function scoped, either by anonymous function or a regular function if we're writing OO code
 * Prefered way is using Anonymous functions and calling them on-the-fly like the following.
 *
 * Note about jQuery: in jQuery, DOM access is only available after document.ready function, so when
 * writing jQuery code, the wrapper should be: $(document).ready(function() {});
 *
 */
 
(function() {
 
    /**
     *   A function in JS is written same as in java.
     *   Note that JS has an issue with scoped variables. No matter where you put the var,
     *   they are instantiated on the start of the function (which can lead to confusing situations)
     *   So it's best to declare all function variables in one var at the start of the function
     */
    function openSecretDoor(code) {
        // declaring all variables in the begining, separated by a comma.
        // variable names are all lowercased, and I prefer to use _ for multiple
        // words
        // last var ends with a ;
 
        var locked           = 1,
            keys             = 'lost em',
            what_will_you_do = 'find a nice hotel';
 
        console.log('Where are my keys ?');    
    }
 
    /**
     * When interacting with the DOM, interactions must happen only after jQuery
     * has finished its initializations - inside the document ready function.
     * Here's a short example
     */
 
    $('document').ready(function() {
        console.log('DOM is ready');
 
        $('body').css('background-color', 'yellow');
    });
 
    /**
     * Objects creation must always use the literal notation to keep
     * the JS demons at bay.
     */
 
    function initSomeObjects() {
        var dog, cat, fish, pets;
 
        // regular objects
        dog  =  { name: 'Lassy', sound : 'Woof' };
        cat  =  { name: 'Garfield', sound: 'Miau' };
        fish =  { name: 'Willie', sound: 'Save me'};
 
        // the fake JS array
        pets = [dog, cat, fish ];
    }
 
}());
(function() {
 
    var p1, p2, p3;
/**
 * Writing OO code in JS is all fun and games, if we consider these simple
 * precautions
 */
 
/**
 * POWER CONSTRUCTOR
 * JS doesn't have the concept of classes. Instead, we build functions
 * that create objects and return them.
 * The use of the 'new' keyword is optional in this context. I prefer not to use it
 *
 * By convention, I use uppercase first letter for power constructors
 */
 
    function Person(name, age) {
        var self = {
            name : name,
            age  : age
        };
 
        // a method is placed as an anonymous function inside a field
        // Note the ; at the end of the function block
        self.greet = function() {
            console.log('Hello. My name is ' + self.name);
        };
 
        return self;
    }
 
    p1 = Person('Jim', 10);
    p3 = Person('Jane', 18);
 
    // this has the same effect but it's a bit confusing, so I don't use it
    p2 = new Person('John', 20);
 
}());