Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

.call javascript

// Call (borrow) an object's function from another object
// 'this' will bind to the borrower
// syntax: ownerObject.ownerFunction.call(borrowerObject)

const sidekick = {
  name: "Robin"
}

const hero = {
  name: "Batman",
  saveGotham: function() {
    console.log(this.name, "is keeping Gotham safe.");
  }
}
hero.saveGotham(); // Batman is keeping Gotham safe.
hero.saveGotham.call(sidekick); // Robin is keeping Gotham safe.
Comment

javscript call

myFunc.call(thisArg, ...args)
Comment

javascript call

function myFunc(p1, p2, pN)
{
     // here "this" will equal "myThis"
}
let myThis = {};

// call myFunc using myThis as context.
// pass params to function arguments.
myFunc.call(myThis, "param1", "param2", "paramN");
Comment

JavaScript Function call()

const person = {
  firstName:"John",
  lastName: "Doe",
  fullName: function () {
    return this.firstName + " " + this.lastName;
  }
}

// This will return "John Doe":
person.fullName();  
Comment

javascript call()

const personOne = {
  firstName : "Elon",
  secondName : "Musk"
}

const getFullName = function(company, country) {
  console.log(this.firstName + " " + this.secondName + ", " + company + ", " + country);
}

const personTwo = {
  firstName : "Mark",
  secondName : "Zuckerburg"
}

getFullName.call(personOne, "Tesla", "United States");    // outputs Elon Musk, Tesla, United States
getFullName.call(personTwo, "Facebook", "United States");    // outputs Mark Zuckerberg, Facebook, United States
Comment

The JavaScript call() Method


		const person = {
name: "John Smith",
getNameAndAddress: function(city, country)
{
return this.name+city+country;
}

}
const personWithoutGet = {
name: "Jerry Smithers"}

 console.log(person.getNameAndAddress.call(personWithoutGet, 'Berlin','Germany'));
/*you can think of bind(), apply() and call() ALL as "stealing someone else's function to use."*/
/* victimBeingStolenFrom.functionName.call(theThief) */
/*notice the second variable personWithoutGet does not have a getName function*/
Comment

javascript call example

l = "abcder";
q = "Ererwrwrewrew";
console.log(String.prototype.substr.call(q, 1, 4))
/*rerw...4 refers to how many characters to substring*/
Comment

PREVIOUS NEXT
Code Example
Javascript :: regex not contains 
Javascript :: remove property from javascript object 
Javascript :: jquery get selected checkbox value array 
Javascript :: event.target data-target 
Javascript :: get status of a user discord js 
Javascript :: js pow 
Javascript :: check value exist in array javascript 
Javascript :: how to reload the window by click on button in javascript 
Javascript :: Properly upgrade node using nvm 
Javascript :: load a config file discordjs 
Javascript :: validate password regex 
Javascript :: jquery count selected options 
Javascript :: copy to clipboard jquery javascript 
Javascript :: nodejs mysql insert query 
Javascript :: prependchild in javascript 
Javascript :: js input type range on hover 
Javascript :: how to use async await inside useeffect 
Javascript :: nested for loop javascript 
Javascript :: is check objet empty 
Javascript :: node read file sync 
Javascript :: indexOf by object key 
Javascript :: javascript assignment operators 
Javascript :: javascript average of arguments 
Javascript :: disable a button in javascript 
Javascript :: for range python javascript 
Javascript :: change the mouse pointer javascript 
Javascript :: access variable outside for loop javascript 
Javascript :: jquery check checkbox 
Javascript :: catch error message js 
Javascript :: how to find the index of an item in nodelist in js 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =