Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript apply

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

// call myFunc using myThis as context.
// destructure array to function arguments.
myFunc.apply(myThis, ["param1", "param2", "paramN"]);
Comment

apply() js

The apply() method accepts arguments in an array:

var arr = [6, 89, 3, 45];
var maximus = Math.max.apply(null, arr); // returns 89
Comment

The JavaScript Apply() Function


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

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

 console.log(person.getNameAndAddress.apply(personWithoutGet, ['Berlin','Germany']));
  }

/*you can think of apply() as "stealing someone else's function to use."*/
/* victimBeingStolenFrom.functionName.call(theThief,[any additional parameters as elements in an array]) */
/*notice the second variable personWithoutGet does not have a getName function*/
 /*the difference between call and apply is that call accepts additional parameters separately while apply (this A) accepts the parameters as an array*/                            
                                 
Comment

JavaScript Function apply()

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

const person1 = {
  firstName: "Mary",
  lastName: "Doe"
}

// This will return "Mary Doe":
person.fullName.apply(person1);
Comment

PREVIOUS NEXT
Code Example
Javascript :: JS function typeof 
Javascript :: how to change css using javascript 
Javascript :: how to mouse hover svg 
Javascript :: how to get max value from array of objects in javascript 
Javascript :: javascript prototype example 
Javascript :: clean my react app 
Javascript :: split the string on any and all periods, question mark using regex 
Javascript :: delete item from a foreach vuejs 
Javascript :: Material-ui clock icon 
Javascript :: callback hell 
Javascript :: programmatically create a custom cron job drupal 7 
Javascript :: react useeffect hook 
Javascript :: react hook form with controlled input 
Javascript :: check if alpine js is loaded 
Javascript :: promise .then javascript 
Javascript :: javascript latitude longitude to km 
Javascript :: Angular Quick Tip: Binding Specific Keys to the Keyup and Keydown Events 
Javascript :: Fake Binary 
Javascript :: get all dates between two dates in moment js 
Javascript :: string to char code array javascript 
Javascript :: register service worker 
Javascript :: appregistry react native 
Javascript :: ternary 
Javascript :: random between min and max 
Javascript :: jquery public function 
Javascript :: cheerio library to parse the meta tags in url 
Javascript :: Javascript function method with any number of arguments 
Javascript :: axios post data vue js 
Javascript :: Angular p-dialog 
Javascript :: mongoose connections 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =