You are on page 1of 4

-----------------------------------------------------FUNCTION AS FIRST CLASS CITIZEN

-----------------------------------------------------// js function can be assigned to a variable


var foo = function() {
alert("Hello from function!");
};
foo();
------------------------------// js function can be passed as a parameter to another function
var foo = function() {
alert("Hello from foo!");
};
Display(foo);
function Display (func_variable) {
func_variable();
};
------------------------------// js function can return a function and can be assigned it to a variable
var bar = OuterMethod();
bar();
function OuterMethod() {
return function InnerMethod() {
alert("InnerMethod");
};
};
-----------------------------------------------------VARIABLE SCOPE
-----------------------------------------------------// Local variable has limited scope
scope_test();
alert(x);
function scope_test() {
var x = "local"
alert(x);
}
------------------------------// Global variable has wider scope, that pervades functions
// explicit or implicit declaration is immaterial
x = "sunil";
var y = "java";
scope_test();
alert(x);
function scope_test() {

alert(x);
alert(y);
}
-----------------------------------------------------INNER FUNCTION
-----------------------------------------------------// Inner/Nested function defines a function within a function
function Addition(value1, value2) {
function Add(operand1, operand2) {
return operand1 + operand2;
}
return Add(value1, value2);
}
alert(Addition(1, 2));
-----------------------------------------------------CLOSURE
-----------------------------------------------------// In a closure, variables in outer function are available to inner function
var add = Addition();
add(20);
function Addition() {
var base = 100;
return function Add(offset) {
alert(offset + base);
}
}
------------------------------//Closure demonstrating data encapsulation
var Status = ChangeState();
Status("ON");
Status("OFF");
Status("XYZ");

function ChangeState() {
var SystemState = "OFF";
return function(value) {
alert("Current State : " + SystemState);
if ((value === "ON" || value === "OFF") && (value != SystemState)) {
SystemState = value;

alert("Changed State : " + SystemState);


}
return SystemState;
};
}
-----------------------------------------------------HOISTING
-----------------------------------------------------var x= 10;
display();
function display (){
alert(x);
};
-------------------------// variable x declaration is accepted, initializtion is not
// output : undefined
display();
function display (){
alert(x);
};
var x= 10;
-------------------------// neither declaration nor initialization is considered
// output : error
display();
function display (){
alert(x);
};
x= 10;
-----------------------------------------------------// 1 st call : x declaration is hoisted [undefined]
// 2nd call : Decl and init considered [10]
display();
function display (){
alert(x);
};
var x= 10;
display();
-----------------------------------------------------Object creation and initializtion
------------------------------------------------------

// js literal object notation


var car = {type:"Fiat", model:"500", color:"white"};
alert(car.model);
-----------------------------------------------------// Create object using object constructor
function person(first, last, age) {
this.firstName = first;
this.lastName = last;
this.age = age;
}
// Initilize object using constructor
var obj = new person("John", "Doe", 50);
alert(obj.firstName);
// Initilize object without constructor
var man = Object.create(person);
man.firstName = 'sunil';
alert(man.firstName);
-----------------------------------------------------// Create object using new operator
var myCar = new Object();
myCar.make = "Ford";
myCar.model = "Mustang";
myCar.year = 1969;
alert(myCar.model);
-----------------------------------------------------// Create empty object and add attributes
var myCar = {};
myCar.make = "Ford";
myCar.model = "Mustang";
myCar.year = 1969;
alert(myCar.model);

You might also like