Created
November 14, 2017 03:29
-
-
Save limabeans/62610eb68ee6fc2cc81b6a7d8341a4e7 to your computer and use it in GitHub Desktop.
call,apply,bind in js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CallApplyBind examples, cuz i occassionally forget call and apply | |
function fn(x,y) { | |
return x+y+this.z | |
} | |
var obj={ z: 4 }; | |
var objfn=fn.bind(obj); | |
// fn.call(thisarg, arg1...) | |
console.log(fn.call(obj,2,3)); | |
// fn.apply(thisarg, [list of args]) | |
console.log(fn.apply(obj, [2,3])); | |
// objfn is fn but with `this` binded to obj | |
console.log(objfn(2,3)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment