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

apply js

Function.prototype.construct = function(aArgs) {
  let oNew = Object.create(this.prototype);
  this.apply(oNew, aArgs);
  return oNew;
};
Comment

apply js

Function.prototype.construct = function (aArgs) {
  let fNewConstr = new Function("");
  fNewConstr.prototype = this.prototype;
  let oNew = new fNewConstr();
  this.apply(oNew, aArgs);
  return oNew;
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: expo react navigation 
Javascript :: How to return arguments in an array in javascript 
Javascript :: component will unmount 
Javascript :: Check propery of an objects array 
Javascript :: tobe a number jest 
Javascript :: redux saga use navigation 
Javascript :: JSX Conditionals: && 
Javascript :: mouse position 
Javascript :: how to subtract time in javascript 
Javascript :: pass setstate to child 
Javascript :: js get current seconds 
Javascript :: js display image from external url 
Javascript :: create a style in div jsx 
Javascript :: js concatenate strings 
Javascript :: axios post method 
Javascript :: Object.Values () javascript 
Javascript :: datatables keep order and page selection page refresh 
Javascript :: react propthpes or 
Javascript :: prevent form submit html javascript jquery 
Javascript :: nuxt get client windows size 
Javascript :: mongodb find and update one field 
Javascript :: js get innertext minus the span text 
Javascript :: how to use nodemailer 
Javascript :: nds npm 
Javascript :: jquery syntax 
Javascript :: arithmetic operators in javascript 
Javascript :: react barcode scanner 
Javascript :: generate unique random number in javascript 
Javascript :: current page number and clicked page number jqery datatables 
Javascript :: back button event listener javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =