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
Function.prototype.bind = function (context) { | |
if (typeof this !== 'function') { | |
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable"); | |
} | |
var fn = this, // 這個 this 代表著被套用 bind 的 function (如 func.bind(this) 那麼 this 就是回傳那個 func) | |
slice = Array.prototype.slice // 暫存 slice 方法 | |
args = slice.call(arguments, 1), // 這邊 call slice 帶入第一次 bind 的 arguments,並將執行環境以外的參數取下 | |
noop = function () {}, // 這個中繼 function 用來提供 prototype chain 連接,假設沒有 Object.create() | |
bound = function () { | |
var ctx = this instanceof noop && context |