Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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*/                            
                                 
Source by www.freecodecamp.org #
 
PREVIOUS NEXT
Tagged: #The #JavaScript #Function
ADD COMMENT
Topic
Name
9+1 =