-
-
Save not-an-aardvark/2f9b6d198de70892db02e171be6184c3 to your computer and use it in GitHub Desktop.
A function that only exists when accessed from strict mode
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
Object.defineProperty( | |
Array.prototype, | |
'flatten', | |
{ | |
configurable: true, | |
set(value) { | |
Object.define(Array.prototype, 'flatten', { configurable: true, writable: true, value }); | |
return value; | |
}, | |
get: function returnUndefinedIfCallerIsSloppy(){ | |
try { | |
returnUndefinedIfCallerIsSloppy.caller.caller; | |
return undefined; | |
} catch (e) { | |
return function flatten() { /* flatten the array */ }; | |
} | |
} | |
} | |
); | |
[].flatten; // -> ƒ flatten() { /* flatten the array */ } | |
(function(){ return [].flatten })(); // -> undefined | |
(function(){ 'use strict'; return [].flatten })(); // -> ƒ flatten() { /* flatten the array */ } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is so incredibly clever 😲