Wednesday, September 21, 2016

JavaScript by Code Examples


Declare and call an anonymous function in one go

(function(arg){
   console.log('called with arg: ' + arg);
})('param');

Output:
called with arg: param

Declare a named function and then call the function

function aFunc(arg){
   console.log('called with arg: ' + arg);
};

aFunc('param');

Output:
called with arg: param

Declare, assign and then call the function

var aFunc = function(arg){
   console.log('called with arg: ' + arg);
};

aFunc('param');

Output:
called with arg: param

Function Closure

aVar = "aVar - global scope";
console.log(aVar);

(function(){
var aVar = "aVar - function scope";
  console.log(aVar);
})();

console.log(aVar);

Output:
aVar - global scope
aVar - function scope
aVar - global scope

Nested Functions

function guid() {
        function s4() {
            return Math.floor((1 + Math.random()) * 0x10000)
              .toString(16)
              .substring(1);
        }
        return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
          s4() + '-' + s4() + s4() + s4();
    }

console.log(guid());

Output:
73a2bfd3-0a2f-bf5a-d029-aec390de6567

Creating function objects with state

var i = function (val){
// this. makes val a property of this object
this.val = val;

// helper function defined in i's scope
function consolePrint(msg){
console.log(msg);
}

this.Print = function() {consolePrint(this.val);}
}
var j = new i(4);  // create a new object based on i
var k = new i(5);  // create another new object based on i
j.Print();
k.Print();

Output:
4
5

Mimicking object-orientation (classes)

// just a normal js function assigned to a variable
var Circle = function (radius){
  this.radius = radius;  // create and assign data member
 };
// Through "prototype", methods declared after the function is initially declared
/    can be automatically
// added to every new instance of the object
Circle.prototype.area = 
  function() { return 22/7 * this.radius * this.radius; }; 
Circle.prototype.circumference = 
  function() { return 2 * 22/7 * this.radius; };

// use the function (class)
var circle1 = new Circle(4);
console.log("circle 1 radius = " + circle1.radius);
console.log("circle 1 area = " + circle1.area());
console.log("circle 1 circumference = " + circle1.circumference());

Output:
circle 1 radius = 4
circle 1 area = 50.285714285714285
circle 1 circumference = 25.142857142857142

Mimicking a Module with a function

// define module and create an instance of the module
var circle = new function(){

   this.area = function(radius){
    return 22/7 * radius * radius;
  };

  this.circumference = function(radius){
    return 2 * 22/7 * radius;
  };

}

// use module
var radius = 4;
console.log("radius = " + radius);
console.log("area = " + circle.area(radius));
console.log("circumference = " + circle.circumference(radius));

Output:
radius = 4
area = 50.285714285714285
circumference = 25.142857142857142

Mimicking a Module with a Json Object

// define module
var circle = {
 "area" : function(radius){
  return 22/7 * radius * radius;
  },
  "circumference" : function(radius){
  return 2 * 22/7 * radius;
  }
}

// use module
var radius  = 4;
console.log("radius = " + radius);
console.log("area = " + circle.area(radius));
console.log("circumference = " + circle.circumference(radius));

Output:
radius = 4
area = 50.285714285714285
circumference = 25.142857142857142

Safe console.log

var debugOn = true;

function log(msg){
    if (debugOn
    &&typeof console != undefined
    && typeof console.log != undefined){
        console.log(msg);
    }
}

log('test');