bind apply code example

Example 1: call apply bind

const obj = { number: 1 }

function foo() {
	console.log(this.number)
}

//bind - binds obj's 'this' context to foo, but doesn't call it
const newFoo = foo.bind(obj) 

//call/apply - binds obj's 'this' context to foo, then calls it
foo.call(obj, /*arg1*/, /*arg2*/) 
foo.apply(obj, [/*arg1*/, /*arg2*/])

//Only difference between call/apply is argument passing - ',' vs '[]'

Example 2: bind in javascript

bind() returns a bound function that, when executed later, will have the correct context ("this") for calling the original function.