-1

In Firefox web console, why can I change the type of name2

var name2=12
undefined

typeof name2
"number"

name2="hello"
"hello"

typeof name2
"string"

and not for name

typeof name
"string"

name=12
12

typeof name
"string"

?

halfer
  • 19,824
  • 17
  • 99
  • 186
Tim
  • 1
  • 141
  • 372
  • 590
  • @CertainPerformance could you explain how the linked post has answers to my question? The linked post doesn't ask the same question, nor have an answer to my question. – Tim May 11 '19 at 03:52
  • The situation is exactly the same - `name` on the top level will always refer to `window.name`, which must always be a string. As an answer there says, `The HTML5 spec requires that window.name is a DOMString` – CertainPerformance May 11 '19 at 06:58
  • After some discussion with @CertainPerformance I'm inclined to agree it is indeed a duplicate. –  May 11 '19 at 07:15
  • @YvetteColomb I would like to know from JS the language perspective: how to understand, implement or simulate a variable which is fixed to a type. Simply saying some language unrelated specification requires so is far from sufficient. Pointy already implied to address that issue (https://stackoverflow.com/questions/56086438/why-cant-i-change-the-type-of-name-in-firefox-web-console#comment98810859_56086450), but CertainPerformance closed the post around the same time, adversely preventing Pointy and other users from participation. – Tim May 11 '19 at 11:59
  • It's not possible with a *standalone variable*, but if you can refer to a property of an object, use getters and setters. Eg, to emulate the behavior of `name` here, have a setter which sets an internal variable to `String(passedVal)`, and a getter which returns that internal variable – CertainPerformance May 11 '19 at 12:01

1 Answers1

0

It's explicitly being cast to a string by JavaScript, since it's a readonly property of the global object (thanks Pointy).

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • Thanks. Can you manually create a variable that works in the same way (as if it is fixed to a type and any value assigned to it will be impliitly converted to the type)? – Tim May 11 '19 at 01:21
  • @Tim A variable that whenever it's updated, is called `toString` upon the new value? – Jack Bashford May 11 '19 at 01:22
  • It's not really a "reserved keyword", it's a non-writable property of the global (`window`) object. – Pointy May 11 '19 at 01:22
  • @Pointy it is writable. see my example, where `name`'s value is changed to `12` – Tim May 11 '19 at 01:22
  • @Tim yea you're right; it just insists on the value being a string. It's not something I encounter in my day to day life :) The point is that it's not a quirk of "JavaScript the language"; it's the runtime context. – Pointy May 11 '19 at 01:24
  • @Pointy do you know how to manually create a variable like that? – Tim May 11 '19 at 01:24
  • @Tim you can't create a *variable* like that, but you can create an object property like that by using [getters and setters](https://stackoverflow.com/questions/812961/getters-setters-for-dummies) – Pointy May 11 '19 at 01:25
  • @Pointy why can't I create a variable like that? Isn't any global variable a property of the global object? – Tim May 11 '19 at 01:26
  • @Tim yes but the global object is not really a JavaScript object. It might be possible in some browsers to use `Object.defineProperty()` to do it; I've never tried. – Pointy May 11 '19 at 01:39
  • Wow Firefox seems fine with it! – Pointy May 11 '19 at 01:42
  • @Pointy What code did you use? – Jack Bashford May 11 '19 at 01:42
  • I'll add an answer; it literally never occurred to me to try this before. *edit* oh wait I can't. Well I just did `Object.defineProperty()` on `window` with a `get:` and `set:` function; the `set` function includes an explicit cast to string, and it behaves pretty much exactly like `name`. Now other browser may or may not like this; IE11 in particular, but I don't know. – Pointy May 11 '19 at 01:42