Skip to content

Instantly share code, notes, and snippets.

@limabeans
Created November 14, 2017 03:29
Show Gist options
  • Save limabeans/62610eb68ee6fc2cc81b6a7d8341a4e7 to your computer and use it in GitHub Desktop.
Save limabeans/62610eb68ee6fc2cc81b6a7d8341a4e7 to your computer and use it in GitHub Desktop.
call,apply,bind in js
// 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