Created
April 2, 2019 21:26
-
-
Save ositowang/b47e480a47ecf633e85901a18022b419 to your computer and use it in GitHub Desktop.
Ultimate version of call() in javascript
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
/** | |
* By checking the passed in context, we add an wrapper to primitive types or | |
* reset the this object to global one if null is passed in | |
* | |
* @param {*} context | |
* @returns | |
*/ | |
Function.prototype.myOwnCallUltimate = function(context) { | |
// Object will tranform primitive value into wrapper Object,kind of Java. | |
context = context ? Object(context) : window; | |
context.fn = this; | |
let args = [...arguments].slice(1); | |
let result = context.fn(...args); | |
delete context.fn; | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment