Skip to content

Instantly share code, notes, and snippets.

@ositowang
Created April 2, 2019 21:26
Show Gist options
  • Save ositowang/b47e480a47ecf633e85901a18022b419 to your computer and use it in GitHub Desktop.
Save ositowang/b47e480a47ecf633e85901a18022b419 to your computer and use it in GitHub Desktop.
Ultimate version of call() in javascript
/**
* 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