Yes, automatic semicolon insertion can be confusing :-)
Modulo whitespace, what you wrote is:
Array.prototype.group = function(by) { console.log("hello", by) }(function()
[1,2,3].group}())
Evaluation starts at the green function call, which evaluates to undefined
because there is no return statement; this is passed as by to the yellow
function call, which logs "hello undefined" and then also evaluates to
undefined, so all this is just the long way of saying:
Array.prototype.group = undefined
The second part of your snippet doesn't care about that:
() => {
[1,2,3].group
console.log('uhm.')
}
will, of course, print "uhm." no matter what, because [1,2,3].group is not
a function call, just a property load whose result is ignored. As you
observe, only when you run [1,2,3].group('foo') will it become apparent
that group is not a function.
If you insert a ";" after the Array.prototype.group = function(by) {...}*;*
assignment, you'll get the behavior you expect.
On Sun, Mar 4, 2018 at 12:08 AM Trevor Linton <[email protected]>
wrote:
>
> <https://lh3.googleusercontent.com/-Ywd-xZFHfoo/Wpuo0d-ZmjI/AAAAAAAACL0/ZEP7Iq_1XTkPtaQWMr1P5bsrG1HPXusBQCLcBGAs/s1600/Screen%2BShot%2B2018-03-04%2Bat%2B1.04.41%2BAM.png>
> Granted, bad practice to add prototypes to primitives, but, still, this is
> strange:
>
> Array.prototype.group = function (by) {
> console.log('hello', by)
> }
>
> (function() {
> [1,2,3].group
> }())
>
> setTimeout(() => {
> [1,2,3].group
> console.log('uhm.')
> },4000)
>
> Executed on node.js by copying it into a file named test.js and running
> node test.js produces:
>
> hello undefined
>
> uhm.
>
>
> Although, executing it in a node repl line produces, just (no hello):
>
> uhm.
>
>
> Executed in chrome dev tools produces:
>
>
> <https://lh3.googleusercontent.com/-Ywd-xZFHfoo/Wpuo0d-ZmjI/AAAAAAAACL0/ZEP7Iq_1XTkPtaQWMr1P5bsrG1HPXusBQCLcBGAs/s1600/Screen%2BShot%2B2018-03-04%2Bat%2B1.04.41%2BAM.png>
>
>
>
> Then finally, if you try and actually run [1,2,3].group('foo') in Safari,
> Firefox both setTimeout version and anonymous function execution context
> succeeds, in node and V8 Chrome it fails with an undefined error.
>
>
> This is wildly and bizarrely inconsistent. It seems overloading
> primitives has bizarre and inconstant behavior, is their a rational
> behavior for this?
>
> --
> --
> v8-dev mailing list
> [email protected]
> http://groups.google.com/group/v8-dev
> ---
> You received this message because you are subscribed to the Google Groups
> "v8-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups
"v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.