Skip to content

Commit

Permalink
feat: {}{} is undefined example
Browse files Browse the repository at this point in the history
adding ```{}{}``` is undefined section
  • Loading branch information
denysdovhan committed Feb 3, 2021
2 parents 04da785 + 9f2d70e commit 619aba1
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion README.md
Expand Up @@ -103,6 +103,7 @@ Currently, there are these translations of **wtfjs**:
- [Same variable redeclaration](#same-variable-redeclaration)
- [Default behavior Array.prototype.sort()](#default-behavior-arrayprototypesort)
- [resolve() won't return Promise instance](#resolve-wont-return-promise-instance)
- [```{}{}``` is undefined](#user-content--is-undefined)
- [📚 Other resources](#-other-resources)
- [🎓 License](#-license)

Expand Down Expand Up @@ -1733,9 +1734,40 @@ thePromise.then(value => {

> This function flattens nested layers of promise-like objects (e.g. a promise that resolves to a promise that resolves to something) into a single layer.
– [Promise.resolve() on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve)
- [Promise.resolve() on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve)

The specification is [ECMAScript 25.6.1.3.2 Promise Resolve Functions](https://tc39.es/ecma262/#sec-promise-resolve-functions). But it is not quite human-friendly.

## `{}{}` is undefined

Write them in console. They will return the value that defined in last object.

```
{}{}; // 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 '{'
```

### 💡 Explanation:

When inspecting each ```{}```, they returns undefined. If you inspect ```{foo: 'bar'}{}```, you will find ```{foo: 'bar'}``` is ```'bar'```.

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

Let's use ```{foo: 'bar'}``` as a block. Write this snippet in your console:

```
if(true) {foo: 'bar'} // 'bar'
```

Surprisingly, it behaviors the same! You can guess here that ```{foo: 'bar'}{}``` is a block.

# 📚 Other resources

Expand Down

0 comments on commit 619aba1

Please sign in to comment.