0

As a fun exercise, I'd like to make a function that looks like a browser native function, so it should look like: function x() { [native code] }

Now, I came real close to figuring it out, as I made the following function: function x() { [native, code] } - it simply creates an array with two elements: native and code, both are undefined. It's pretty useless, but it looks just like a native function, except for the comma.

So I began looking for a way to eliminate this comma, and my first thought was using some kind of character that looks like a space but isn't really functioning as one (since JS now supports unicode), but it seems like every space-like character I found is actually functioning as a regular space.

So, does anyone as an idea how can I eliminate this comma and make the function look like a native function?

Please don't write stuff like "override toString" or "you can use space if you use brackets" because that's obviously not what I'm trying to achieve.

Eliran Pe'er
  • 2,569
  • 3
  • 22
  • 33
  • 1
    I dont understand you at all - what is the "browser native function" and how can I find out, if something looks like "browser native function" or not? – libik Feb 25 '18 at 17:34
  • @libik For example `alert.toString()` returns `function alert() { [native code] }`. OP’s trying to make a function that has just this as an output when stringified, though not by overriding `toString`. – Sebastian Simon Feb 25 '18 at 17:37
  • @libik Open Chrome's console and type in: `[].push` - you'll see the following output: `ƒ push() { [native code] }` - That's how you know a function is a native browser function, meaning it was implemented by the browser. If you try to print a function you created, the output will be its code. – Eliran Pe'er Feb 25 '18 at 17:37
  • I think, `[native code]` purposely has a space so that no JS function _can_ look like a native function, if `toString` is not overridden. – Sebastian Simon Feb 25 '18 at 17:39
  • @Teemu Sorry, but that's incorrect. You can use any unicode character in JS variables. See here: https://mathiasbynens.be/notes/javascript-identifiers – Eliran Pe'er Feb 25 '18 at 18:07
  • Indeed, something new ... Though "_Some of these don’t work in all browsers/environments ..._" – Teemu Feb 25 '18 at 18:12
  • Doesn't matter for the sake of the question – Eliran Pe'er Feb 25 '18 at 18:15

1 Answers1

4

function x() { [nativeᅠcode] }

console.log( x )

https://stackoverflow.com/a/48274520/1383168


Another option from https://davidwalsh.name/detect-native-function#comment-499278, but no name:

function x() { return 42; }

x = x.call.apply(x.bind, [x]);

console.log( x )
console.log( x() )
Slai
  • 22,144
  • 5
  • 45
  • 53
  • Thanks, that indeed looks good on my Android, however, on my Mac I can't see the space (it looks like: `nativecode`) :( any idea why? – Eliran Pe'er Feb 25 '18 at 18:08
  • 1
    @EliranPe'er I am guessing it might depend on the font and the Unicode support. I don't know of any other whitespace characters that can be used in identifier, but I am guessing there might be more. – Slai Feb 25 '18 at 18:10
  • I now see it on Windows and it works! awesome! your second example doesn't work though, Chrome seems to trace it back to the original function. – Eliran Pe'er Feb 25 '18 at 20:02