Skip to content

Instantly share code, notes, and snippets.

View ericboy0224's full-sized avatar
🏠
WFH

Jia-Hao Lin ericboy0224

🏠
WFH
  • Saviah
  • Taiwan
View GitHub Profile
@ericboy0224
ericboy0224 / bind.js
Last active June 15, 2022 09:40 — forked from yyx990803/bind.js
implementing Function.prototype.bind
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