jsfn:understanding_call-and-apply_invocation

Call & Apply (indirect invocation)

  1. permet de définir la valeur de l'argument this
  2. Control: this and arguments
  3. Call passe une valeur, Apply un array
var speak = function (what) {
  console.log(this.love)
}
var saySomething = {normal: "meow", love: "purr"} // ça pourrait être des fonctions en attributs
speak.call(saySomething); // purr
var speak = function (what) {
  console.log(what)
}
var saySomething = {normal: "meow", love: "purr"}
speak.call(saySomething, saySomething.normal); // le deuxième argument va devenir le what plus haut ! // meow

var speak = function (what) {
  console.log(what)
  console.log(this.love)
}
var saySomething = {normal: "meow", love: "purr"} // ça pourrait être des fonctions en attributs
speak.apply(saySomething, ["meouff"]); // meouff, purr
jsfn/understanding_call-and-apply_invocation.txt · Last modified: 2016/01/31 20:53 by leo