Created
November 30, 2015 12:04
-
-
Save kopiro/6000c4e1b399041107ac to your computer and use it in GitHub Desktop.
Call and Apply replica in Pure JS
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 x(a,b,c) { | |
console.log('Called X:', a,b,c); | |
} | |
Function.prototype._call = function() { | |
var first = Array.prototype.shift.apply(arguments); | |
this.apply(first, arguments); | |
}; | |
Function.prototype._apply = function() { | |
var first = Array.prototype.shift.call(arguments); | |
var str = []; | |
for (var i = 0; i < arguments[0].length; i++) str.push('arguments[0]['+i+']'); | |
eval('this.call(first,' + str.join(',') + ')'); | |
}; | |
x._apply(null, [1,2,3]); | |
x._call(null, 1,2,3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment