8000 adding `{}[] ` returns array section by hsl0 · Pull Request #165 · denysdovhan/wtfjs · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

adding {}[] returns array section #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 0 commits into from
Closed

adding {}[] returns array section #165

wants to merge 0 commits into from

Conversation

hsl0
Copy link
Contributor
@hsl0 hsl0 commented Oct 27, 2020

I found this simple strangeness of javascript!

I have tested it on Chromium Edge, Chrome, Firefox and IE.
In node.js, {}[] returns array, but {foo: 'bar'}['foo'] returns 'bar'

@libook
Copy link
8000 Contributor
libook commented Oct 28, 2020

a={foo: 'bar'}['foo'] // a='bar'
a={}[] // Uncaught SyntaxError: Unexpected token ']'
{foo: 'bar'}[] // []
a={foo: 'bar'}[] // Uncaught SyntaxError: Unexpected token ']'

{foo: 'bar'}['foo'] means read the value of 'foo' key from object {foo: 'bar'}.
But if there is no item in the array, they will be one emty object {} and one empty array [] for JS engine. Just like {};[];.

> (()=>({}[]))()
(()=>({}[]))()
         ^

Uncaught SyntaxError: Unexpected token ']'


> (()=>({}['foo']))()
undefined


> (()=>({foo:'bar'}['foo']))()
'bar'


> (()=>({foo:'bar'}[]))()
(()=>({foo:'bar'}[]))()
                  ^

Uncaught SyntaxError: Unexpected token ']'

I did some "return test" with functions. You can see:
It was not {}[] returned []; it was [] returned [].

@hsl0
Copy link
Contributor Author
hsl0 commented Oct 28, 2020

@libook hadn't mean "{}[] returns array, but {foo: 'bar'}['foo'] returns 'bar' in node.js" is strange, but I meant "{}[] returns array and {foo: 'bar'}['foo'] returns array in browser console" is strange as I wrote in README.

Anyway, thanks for letting me know how this happened! You didn't answer the intended question, but the answer could explain my curiosity.

I want to know one more thing, how {}{} returns undefined in console, instead of returning object or throwing error? Is it strange behavior? (Chromium, node)

{}{}; // undefined
{}{}{}; // undefined
{}{}{}{}; // undefined
{foo: 'bar'}{}; // 'bar'
{}{foo: 'bar'}; // 'bar'
{}{foo: 'bar'}{}; // 'bar'
{a: 'b'}{c:' d'}{}; // 'd'
{a: 'b', c: 'd'}{}; // SyntaxError: Unexpected token ':'
({}{}); // SyntaxError: Unexpected token '{'

@libook
Copy link
Contributor
libook commented Oct 29, 2020

Yes, they are strange.

There are 2 meaning for {}: object and block.
For example, the {} in ()=>{} means block. So we need to use ()=>({}) to return an object.
So there are 2 questions:

  1. For each expression, it may not return one single value . Where did REPL read the final result?
  2. For each {}, is it block or object?

Also, REPL is different with directly executing files.

@hsl0
Copy link
Contributor Author
hsl0 commented Oct 30, 2020

@libook Then, can it be recorded in WTFJS?

@hsl0 hsl0 changed the title adding {}[] returns array section adding {}{} Oct 30, 2020
@hsl0 hsl0 closed this Oct 30, 2020
@hsl0 hsl0 changed the title adding {}{} adding {}[] returns array section Oct 30, 2020
@denysdovhan denysdovhan added the question Questions from readers label Feb 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Questions from readers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0