Skip to content

Instantly share code, notes, and snippets.

@W-Mills
Created August 20, 2019 20:59
Show Gist options
  • Save W-Mills/52fd27f30ea242524e26892bb64c443c to your computer and use it in GitHub Desktop.
Save W-Mills/52fd27f30ea242524e26892bb64c443c to your computer and use it in GitHub Desktop.
Clairifying this in javascript example 3
const foo = {
bar: 10,
multiplyByBar: function(...args) {
args.forEach(arg => console.log(arg * this.bar));
},
};
foo.multiplyByBar(5); // logs 50
foo.multiplyByBar(5, 10, 15) // logs 50, then 100, then 150
const qux = foo.multiplyByBar;
qux(5) // logs NaN (context loss)
qux.call(foo, 5) // logs 50
qux.call(foo, 5, 10, 15) // logs 50, then 100, then 150
qux.apply(foo, [5]) // logs 50
qux.apply(foo, [5, 10, 15]) // logs 50, then 100, then 150
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment