Created
July 26, 2020 06:42
-
-
Save sudikrt/1b2b61e2fcf1554db20cfdb866c9ae75 to your computer and use it in GitHub Desktop.
call, apply and bind method in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let name = { | |
firstName : 'sudar', lastName : 'Test', | |
printFullName : function () { | |
console.log (`${this.firstName}-${this.lastName}`) | |
} | |
} | |
name.printFullName(); | |
//Function Borrowing | |
let name = { | |
firstName : 'sudar', lastName : 'Test', | |
printFullName : function () { | |
console.log (`${this.firstName}-${this.lastName}`) | |
} | |
} | |
name.printFullName(); | |
let name1 = { | |
firstName : 'Sac', lastName : 'Test' | |
} | |
//function Borrowing | |
name.printFullName.call (name1); | |
//Function Borrowing ends | |
//Keepig function saperate | |
let name = { | |
firstName : 'sudar', lastName : 'Test' | |
} | |
let printFullName = function () { | |
console.log (`${this.firstName}-${this.lastName}`) | |
} | |
let name1 = { | |
firstName : 'Sac', lastName : 'Test' | |
} | |
printFullName.call (name); | |
printFullName.call (name1); | |
//Function call with extra params | |
let name = { | |
firstName : 'sudar', lastName : 'Test' | |
} | |
let printFullName = function (homeTown) { | |
console.log (`${this.firstName}-${this.lastName}-${homeTown}`) | |
} | |
let name1 = { | |
firstName : 'Sac', lastName : 'Test' | |
} | |
printFullName.call (name , 'karnataka'); | |
printFullName.call (name1, 'test') | |
//Apply method | |
let name = { | |
firstName : 'sudar', lastName : 'Test' | |
} | |
let printFullName = function (homeTown, state) { | |
console.log (`${this.firstName}-${this.lastName}-${homeTown}-${state}`) | |
} | |
let name1 = { | |
firstName : 'Sac', lastName : 'Test' | |
} | |
printFullName.call (name , 'karnataka'); | |
//function Borrowing | |
printFullName.call (name1, 'test'); | |
//apply pass list of args | |
printFullName.apply (name , ['karnataka','Blr']); | |
printFullName.apply (name1, ['test','Maha']); | |
//Apply Ends | |
// Bind Method | |
let name = { | |
firstName : 'sudar', lastName : 'Test' | |
} | |
let printFullName = function (homeTown, state) { | |
console.log (`${this.firstName}-${this.lastName}-${homeTown}-${state}`) | |
} | |
let name1 = { | |
firstName : 'Sac', lastName : 'Test' | |
} | |
printFullName.call (name , 'karnataka'); | |
//function Borrowing | |
printFullName.call (name1, 'test'); | |
//apply pass list of args | |
printFullName.apply (name , ['karnataka','Blr']); | |
printFullName.apply (name1, ['test','Maha']); | |
//Create a copy of printFullName and bind it to name obejct with extra params it will return a method which can be used later | |
let printMyName = printFullName.bind(name, 'test','Param2'); | |
printMyName(); | |
//Bind Ends |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment