Last active
March 30, 2023 01:59
-
-
Save cowboy/4477847 to your computer and use it in GitHub Desktop.
JavaScript: call invo-cursion?
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
// OOP | |
console.log( 'OHAI'.blink() ); | |
// Call invocation | |
console.log( String.prototype.blink.call('OHAI') ); | |
// $ always makes things look awesome. | |
var $ = Function.prototype.call; | |
// Very explicit call invocation | |
console.log( $.call(String.prototype.blink, 'OHAI') ); | |
// Very, very explicit call invocation, ie. call invo-cursion? | |
console.log( $.call($,$,$,$,$,$,$,$,$,$,$,$, String.prototype.blink, 'OHAI') ); | |
// ^^^^^^^^^^^^^^^^^^^^^^^ "bonus" calls | |
// You can have fun with apply invocation and _ too. | |
var _ = Function.prototype.apply; | |
// Very, very explicit apply invocation, ie. apply invo-cursion. | |
console.log( _.apply(_,[_,[_,[_,[_,[_,[_,[_, [ String.prototype.blink, ['OHAI'] ]]]]]]]]) ); | |
// ^^^^^^^^^^^^^^^^^^^^^^ "bonus" applies, and fun w/brackets ^^^^^^^^ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@jackocnr For the last FYI, I thought it would be like this: when you pass
Number.call(or Function.call)
to Array.prototype.map, theNumber.call(or Function.call)
is no longer an Object method, it is now just a simple function, with thecall
missingthis
then JavaScript runtime call anundefined
function and will throw an error.For better understanding, here is another example: