Wednesday, November 14, 2012

Javascript Note 2

  1. arguments special variable available in the scope of a function. It contains all the arguments passed to a function. For example,
    function p() {
       ...
    }
    function q(x) {
       ...
    }
    
    p(1,2,3,4);   // arguments.length = 4
    q(1,2,3,4);   // arguments.length = 4.
                  // Ie, the first argument is not swallowed by x.
    

  2. Function.prototype.call(t[, args]*). Calls the function passing along args in a content where this refers to t.
  3. Function.prototype.apply(t, arg). Same as call except the argument is an array. Coming from R, I had thought this meant the function was applied to each element of arg.
  4. Array.prototype.map(f). Applies function f to each element of the array, returning a new array.
  5. Array.prototype.forEach(f). Applies function f to each element of the array but returns undefined. In other words, basically used only for side effects such as printing to screen, writing to filesystem, etc.

Be aware that
1 + "5";   // "15"
"1" + 5;   // "15"
"5" & 20;  // 0 
"5" & 21;  // 5
"1" & "a"; // 0   (Octal of ASCII "1" is 061)
"1" & "b"; // 0

var x = { 1:"b", "5":"e" };
x["1"] == x[1];  // TRUE
x[5] == x["5"];  // TRUE



No comments:

Post a Comment