Context call, apply, bind - 401-advanced-javascript-jv/seattle-javascript-401d30 GitHub Wiki

Context in JavaScript is not a difficult concept to work with, but can behave oddly. The keyword this can have different meanings depending on where and how it gets used. When used in the global scope of a front-end script, this means the window object. When used in the scope of a method on an object, this refers to the object where that method lives.

Context can be changed in a few ways, all of which are methods called on the object. Those methods are call(), apply(), and bind().

call(newCtx[, a[, b[, ...]]]): This method will run a specified function within the context that it's given, meaning this becomes the object passed in as the first parameter to call. Any subsequent parameters are all passed directly to the called function.

apply(newCtx[, a]): This method is nearly identical to call, except it takes a maximum of two parameters. The parameter is the new context, and the second parameter is an array, which are parameters that get passed to the called function.

bind(): This method returns a copy of the function on which it was called, and also changes this for that function. this is changed to the same context where bind() was called.