Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

apply call in bind method()

var car = {
    registrationNumber: "FT5142",
    brand: "Benz",
}

  function displayDetails(ownerName){
        console.log(ownerName + ", this is your car: " + this.registrationNumber+ " " + this.brand );
    }
// car.displayDetails();


displayDetails.apply( car, ["Raymund"]);

displayDetails.call(car, "Raymund");
Comment

call apply and bind method in javascript

* call apply and bind

- This concept is called function borrowing 
- We can borrow function from the other objects 
  and use the data with the other object.

-> Call invokes the function and allows you to pass in arguments
 one by one.
-> Apply invokes the function and allows you to pass in arguments 
 as an array.
-> Bind returns a new function, allowing you to pass in a
 this array and any number of arguments.


let myName = {
  firstname: "Abhishek",
  lastname: "Bhavsar",
}
let printFullName = function (hometown, state) {
  console.log("=>>>>>>>", this.firstname + " " + this.lastname + "from" + hometown + "," + state)
}
// call 
printFullName.call(myName, "Ahmedabad", "Gujrat");

let name2 = {
  firstname: "Sachin",
  lastname: "Tendulkar",
}
// function borrowing
printFullName.call(name2, "Mumbai", "Maharashtra");

// apply 
printFullName.apply(name2, ["Mumbai", "Maharashtra"]);

//  bind method
let printMyName = printFullName.bind(name2, "Mumbai", "Maharashtra");
console.log(printMyName);
printMyName();
Comment

call,bind and apply in javascript

// ----------------------
// Traditional Example
// ----------------------
// A simplistic object with its very own "this".
var obj = {
    num: 100
}

// Setting "num" on window to show how it is NOT used.
window.num = 2020; // yikes!

// A simple traditional function to operate on "this"
var add = function (a, b, c) {
  return this.num + a + b + c;
}

// call
var result = add.call(obj, 1, 2, 3) // establishing the scope as "obj"
console.log(result) // result 106

// apply
const arr = [1, 2, 3]
var result = add.apply(obj, arr) // establishing the scope as "obj"
console.log(result) // result 106

// bind
var result = add.bind(obj) // establishing the scope as "obj"
console.log(result(1, 2, 3)) // result 106
Comment

call bind apply in javascript

function Car(type, fuelType){
	this.type = type;
	this.fuelType = fuelType;
}

function setBrand(brand){
	Car.apply(this, ["convertible", "petrol"]); //Syntax with array literal
	this.brand = brand;
	console.log(`Car details = `, this);
}

function definePrice(price){
	Car.apply(this, new Array("convertible", "diesel")); //Syntax with array object construction
	this.price = price;
	console.log(`Car details = `, this);
}

const newBrand = new setBrand('Brand1');
const newCarPrice = new definePrice(100000);
Comment

PREVIOUS NEXT
Code Example
Javascript :: use navigate in class component react native 
Javascript :: uppercase first letter javascript 
Javascript :: password validation with regular expression in javascript 
Javascript :: jQuery load() Method 
Javascript :: async await iife 
Javascript :: Square Every Digit 
Javascript :: upload file axios 
Javascript :: js in_array 
Javascript :: map method in react 
Javascript :: chrome storage set example 
Javascript :: Start Express Properly 
Javascript :: pm2 logs on same console 
Javascript :: how to get current template in vuejs 
Javascript :: image react 
Javascript :: cors problem node js 
Javascript :: delete from list javascript 
Javascript :: how to add a white space in jsx 
Javascript :: what does document.getelementbyid return 
Javascript :: eslint disable next line multiple rules 
Javascript :: 1 dollar in rupees 
Javascript :: route parameter in node 
Javascript :: jest render target container is not a dom element 
Javascript :: react native webview postmessage example 
Javascript :: how to make a grocery list in javascript 
Javascript :: completablefuture async example 
Javascript :: jquery selector class child 
Javascript :: js add class to html 
Javascript :: use inline and other styles react native 
Javascript :: ngfor only x item 
Javascript :: react context api with hooks 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =