Skip to content

Instantly share code, notes, and snippets.

@zthxxx
Created June 17, 2019 05:06
Show Gist options
  • Save zthxxx/e37af0bf0c21326fc84e99d774c91952 to your computer and use it in GitHub Desktop.
Save zthxxx/e37af0bf0c21326fc84e99d774c91952 to your computer and use it in GitHub Desktop.
simulate JS native call and bind
Function.prototype.call = function (context, ...args) {
context = context === undefine || context === null ? window : Object(context)
const fn = Symbol('fn')
context[fn] = this
const result = context[fun](...args)
delect context[fn]
return result
}
Function.prototype.apply = function (context, args = []) {
return this.call(context, ...args)
}
Function.prototype.bind = function (context, ...bindArgs) {
return (...args) => this.call(context, ...bindArgs, ...args)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment