Javascript 小记

1. !! means Coerces oObject to boolean. If it was falsey (e.g. 0, nullundefined, etc.), it will be false, otherwise, true.

2. check what device it is

function isPC() {
    var system = {
        win : false,
        mac : false,
        xll : false
    };
    //检测平台
    var p = navigator.platform;
    system.win = p.indexOf("Win") == 0;
    system.mac = p.indexOf("Mac") == 0;
    system.x11 = (p == "X11") || (p.indexOf("Linux") == 0);
    //跳转语句
    if (system.win || system.mac || system.xll) { //转向电脑端
        return true; //是电脑
    } else {
        return false; //是手机
    }
}

 3. if a nested function is invoked as a method, its "this" value is the object it was invoked on. If a nested function is invoked as a function, then "this" value will be either the global object(non-strict mode) or undefined(strict-mode)

var o = {                             // An object o.
    m: function() {                 // Method m of the object
        var self = this;             // Save the this value in a variable
        console.log(this === o); // prints "true" 
        f();
        
        function f(){
            console.log(this === o);  // "false": this is global or undefined
            console.log(self === o);  // "true": self is the outer of this value
        }
    }
};

 4. Indirect Invocation:

call() ---> this method uses its own argument list as arguments to the function 

apply() ---> this method expects an array of values to be used as arguments

5. lexical scoping ---> defines how variable names are resolved in nested functions: inner functions containt the scope of parent functions even if the parent functions has returned.

the functions are executed using the variable scope that "was" in effect when they were defined, not the variable scope that "is" in effect when they are invoked.

posted @ 2015-04-28 08:34  Garret_lee  阅读(144)  评论(0编辑  收藏  举报