1

Try this syntax in console

 {age:15}toString()  
 '[object Undefined]' // this is the return

why this happens it looks syntactically wrong so I'm not sure how the interpreter parse it ?

the object {age:15} looks like completely ignored ? is that true ?

if not ignored dose it mean it garbage as soon as it created I'm just thinking but not sure

Ayman Morsy
  • 1,279
  • 1
  • 10
  • 28
  • ofc it will throw an error unless u meant to type `{age:15}.toString()` – Chris G Feb 28 '23 at 12:26
  • @ChrisG doesn't. It's syntactically correct. – VLAZ Feb 28 '23 at 12:27
  • 5
    `toString()` on its own gets you the exact same result. – CBroe Feb 28 '23 at 12:27
  • @CBroe I know but I didn't know the first part how it parsed – Ayman Morsy Feb 28 '23 at 12:28
  • Interesting. Looks like `{ age: 15 }` and `toString()` are evaluated as two separate statements. – MC Emperor Feb 28 '23 at 12:29
  • 1
    I guess the curly brackets serve as instruction separator here. You could make that `{age:15};toString()` to make it more obvious, but the result is the same. – CBroe Feb 28 '23 at 12:29
  • `Uncaught SyntaxError: Unexpected identifier 'toString'` – Chris G Feb 28 '23 at 12:29
  • 2
    Wait so `age:15;toString()` would also work. Interesting. – Reyno Feb 28 '23 at 12:30
  • 5
    `{...}` is a [block statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/block), `age:` is a [label](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label) and `15` is just a stand-alone number literal. It's syntactically correct, but doesn't make much sense logically. – Ivar Feb 28 '23 at 12:33
  • @ChrisG your interpreter is broken. The code is valid. – VLAZ Feb 28 '23 at 12:33
  • @Reyno Yeah. `age:15` is just a labelled statement that doesn't do anything. If you wrap it in a block it still doesn't do anything. No matter how much it's changed, while it's inconsequential to the code, it won't change things. Which I guess is a bit of a tautology. – VLAZ Feb 28 '23 at 12:41
  • @VLAZ I figured it was a block followed by a `toSring()` call but I forgot about the label statement since it is rarely used, hence it confused me. You'll learn something everyday – Reyno Feb 28 '23 at 13:19

1 Answers1

5

{age:15}:

  1. Is a block. It is not an object. For more information see What is the explanation for these bizarre JavaScript behaviours mentioned in the 'Wat' talk for CodeMash 2012? and in particular the explanations for {} + [] and {} + {} that deal with the same block behaviour.
  2. It contains a label with identifier age which is then followed by the expression-statement 15. For more see What does the JavaScript syntax foo: mean?
  3. It is irrelevant to the rest of the code.

The actual output here is only from toString():

console.log(toString());

With that in mind let's examine the output: toString() is a method on the object prototype. Thus pretty much all objects have it. Including window:

console.log("toString" in window); //true

Calling just toString() means the method will be picked up from the global object - window.

Because the value of this is determined at call time and implicitly there is no object to pick it from:

A normal call would look like someObject.toString() which will set the this value to someObject. It is also equivalent to calling Object.prototype.toString.call(someObject) which will explicitly set the value of this to `someObject.

However with the entire toString() is basically equivalent to Object.prototype.toString.call(undefined) as there is nothing the method is called on.

console.log(Object.prototype.toString.call(undefined))
VLAZ
  • 26,331
  • 9
  • 49
  • 67