Call, apply, bind

Basic program to understand the methods

by M Rahul Reddy

JavaScript

// ************* for bind *************
console.clear();
console.log("Bind() introduced in EcmaScript5");
var obj = {name:"Rahul"};

var greeting = function(a,b,c){
    return "welcome "+this.name+" to "+a+" "+b+" in "+c;
};

var me = greeting.bind(obj);
console.log(me("Newtown","India","Asia"));

// ************* for apply *************
console.log("Apply() introduced in EcmaScript3");
var obj = {name:"Rahul"};

var greeting = function(a,b,c){
    return "welcome "+this.name+" to "+a+" "+b+" in "+c;
};

console.log(greeting.call(obj,"Newtown","India","Asia"))

// ************* for call *************
console.log("Call() introduced in EcmaScript3");
var obj = {name:"Rahul"};

var greeting = function(a,b,c){
    return "welcome "+this.name+" to "+a+" "+b+" in "+c;
};

console.log(greeting.apply(obj,["Newtown","India","Asia"]))