Tuesday 21 January 2014

Javascript Quick Recap

Values, variables and literals

    • Variables
      • var a ;  << Default value  of a is undefined and behave as false
        • undefined + numeric = NaN
          var a; a+2  => NaN
        • var a; if(!a) alert("hi"); => alert("hi");
      • use of undeclared variable  result in  ReferenceError exception.
      • Global variables
        • Global variables are infact properties of global objects. eg window in web pages and can be accessed as window.variable
    • Constants
      • const prefix = '212' ;  // Read only variables
    • Concatenation 
      • using  + operator
        "32" + " is a number" =>  "32 is a number"
      • toString()
        • var a = 32;   a.toString() => "32"
    • Type conversion
      • unary operator + will convert the right hand side operand to int
        • +document.getElementById("div0").value
        • 23 == +"23"  => true
      • parseInt() , parseFloat()
    • literals
      • Array
        • var arr = [2, 3 , , "dfd", ]
          • length: 4 // last trailing comma is ignore.
          • empty place will be treated as undefined
        • Important link:
          https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Predefined_Core_Objects
      • Object

        var foo = {a: "alpha", 2: "two"};
        console.log(foo.a);    // alpha
        console.log(foo[2]);   // two
        //console.log(foo.2);  // Error: missing ) after argument list
        //console.log(foo[a]); // Error: a is not defined
        console.log(foo["a"]); // alpha
        console.log(foo["2"]); // two
      • Enumerating all properties of an object
        • for..in loops
          • for(var key in obj)  console.log( obj[key] );
        • Object.keys(o) => returns an array of keys
          • var arrKeys = Object.keys(obj);  console.log(  obj[arrKeys[0] );
        • Object.getOwnPropertyNames(o) => returns array of values
      • Iterator and Generator
        • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators
      • Defining getters and setters
        • defined using object initializers, or
        • added later to any object at any time using a getter or setter adding method
        • get : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/get?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FOperators%2Fget
        • For deleting get/set use delete

Class ,oops 

    • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Details_of_the_Object_Model
    • Inheritance
      • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_Revisited

Using call to invoke an anonymous function

In this purely constructed example, we create anonymous function and use call to invoke it on every object in an array. The main purpose of the anonymous function here is to add a print function to every object, which is able to print the right index of the object in the array. Passing the object as this value was not strictly necessary, but is done for explanatory purpose.
var animals = [
  {species: 'Lion', name: 'King'},
  {species: 'Whale', name: 'Fail'}
];

for (var i = 0; i < animals.length; i++) {
  (function (i) { 
    this.print = function () { 
      console.log('#' + i  + ' ' + this.species + ': ' + this.name); 
    } 
    this.print();
  }).call(animals[i], i);
}

References:

  • Core Objects :  https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Predefined_Core_Objects
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide