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 :: The .querySelector() Method 
Javascript :: how to sepaarte text in object javascript 
Javascript :: use cors 
Javascript :: javascript loop last index 
Javascript :: change text based on dropdown selection javascript 
Javascript :: mdn destructuring 
Javascript :: for of and for in javascript 
Javascript :: knex.raw postgres how to add multiple parameters 
Javascript :: js ctx dash line 
Javascript :: how to check is the key of a localstorage is emopty 
Javascript :: timeout 30000 milliseconds 
Javascript :: javascript get the screen color depth 
Javascript :: nextjs link image 
Javascript :: react state management 
Javascript :: change cover photo with javascript 
Javascript :: factorial bigger than 170 javascript 
Javascript :: Expo camera rotation 
Javascript :: discord.js v13 joinVoiceChannel 
Javascript :: js variables 
Javascript :: lodash uniqby 
Javascript :: ReactDOM render in v18 
Javascript :: loop map with key value pair js 
Javascript :: if in javascript 
Javascript :: stack overflow js bubble sort 
Javascript :: send object from laravel to react js component 
Javascript :: javascript multiple cases 
Javascript :: private methods js 
Javascript :: check internet connection in react 
Javascript :: angular size of array 
Javascript :: check if localstorage is undefined 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =