Created
June 17, 2019 05:06
-
-
Save zthxxx/e37af0bf0c21326fc84e99d774c91952 to your computer and use it in GitHub Desktop.
simulate JS native call and bind
This file contains hidden or 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.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