Function 函式
Categories:
Callback Function
什麽是 Callback Function?
用函式当作参数放进另外一个函式,透过另一个函式来呼叫它
A callback function is a function passed into another function as an argument, so a callback is a function will be executed after parent function is executed
什麽是 Callback hell
当在函式的参数中有多个 callback 函式时,会导致连续呼叫 callback 变成 callback hell
this
什麽是 bind, call, apply
js 中 this 是动态的,所以会需要这些好方法让函式怎麽被呼叫都能有固定的 this。
.bind() 让 function 在呼叫前先绑定某个物件,不会马上执行。
.call() 跟 .apply() 都是马上去执行这个 function ,并将此 function 的 this 替换成第一个参数带入的物件。他们唯一的不同是传入参数的方法,.call() 是由「逗点」隔开,而 .apply() 则是传入阵列作为参数。
They allow us to borrow functions and set the this value in function invocation.
.bind allow you to execute the function in the future because it returns a new function that fixes the context this and first arguments if given.
.call and .apply will execute the function immediately. The difference are.callaccept subsequent arguments but .applyexpects the second argument to be an array