3

Regarding objects, why do they have keys that are associated with their data. For instance: a variable with the identifier 'num' will hold a reference to that value in memory, but why do objects not use this principle? Instead they have keys that are, correct me if I am wrong, actual values themselves that are associated with a property of an object. I hope I am explaining this correctly. In short, why do objects not use identifers like standard variables and instead have keys associated with them that are their own values. I checked it and JS says they are of type 'string'. Standard variables have an identifier that is not a value but a reference to said value, why do objects have this additional layer of complexity?

Why do objects behave like this-

Key variable associated with variable

And not like this (like normal declaration with let, var, const)-

Identifier references variable

Note: I am new to JS

Mysasas
  • 91
  • 5
  • Because that's what an object is for -- it's a collection of named values. – Barmar Oct 29 '21 at 17:21
  • You can only have one variable named `foo`, buy you can have multiple objects with the key `foo`. – Barmar Oct 29 '21 at 17:21
  • @barmar nope. You can have exactly one variable with the same name in an environment record, just as an object can only have a key only once. In fact there are environment records that are also objects (c.f. `window`) – Jonas Wilms Oct 29 '21 at 17:24
  • but is there a benefit to using keys over identifiers? the key is acting like an identifier but I do not see why there is this added layer when regular variable declarations simply hold the reference in an identifer – Mysasas Oct 29 '21 at 17:25
  • Here is a basic explanation of data types in javascript: https://blog.devmountain.com/what-are-data-types-javascript-101/ Each data type is for a different use to increase the flexibility of the possible code and ease of writing that code. – DrCord Oct 29 '21 at 17:25
  • @Mysasas - You can answer your own question by writing a small application that relies on a non-trivial data structure. Write an application that requires you to store attributes of several automobiles and then prints those to the console. This will help you understand the value of key/value pairs. – Randy Casburn Oct 29 '21 at 17:30
  • 1
    @mysasas an identifier is just a term for a name that appears in the program code. E.g. `a.b` is an expression with two identifiers, the later one could also be expressed through a string literal `a["b"]`. The main advantage of having strings as object keys is that they can be computed at runtime, whereas an identifier is a fixed part of the program – Jonas Wilms Oct 29 '21 at 17:31
  • Why *should* object only accept valid variable identifiers? You're asking why they don't but I really want to flip that question on you because that seems to imply that there is some benefit to that and the current situation negates it, yet I don't really see it. – VLAZ Oct 29 '21 at 17:36
  • @VLAZ Let's flip again, why should variables only have fixed names (`let name = "dynamic"; with({ [name]: 1 }) { console.log(eval(name)); }`)? ;) – Jonas Wilms Oct 29 '21 at 17:40
  • @JonasWilms I literally linked to ["Variable" variables in JavaScript](https://stackoverflow.com/q/5187530) twice within the last 10-ish minutes. One of those was dupe hammered... Please, no more :/ – VLAZ Oct 29 '21 at 17:42
  • but why do regular variables not use keys then, e.g. let num = 5; num is the identifier but it has no key whereas object num: 5 num becomes a string. I just wabted to know why it is different for objects variables and not standard variables – Mysasas Oct 29 '21 at 18:01
  • There is no difference. Take for example `var test = 1; console.log( window[ "test" ] );` ... Here we refer to the same value both through an identifier `test` and a string literal `"test"`. There is really not much difference conceptually between variable names and object keys (there is though in the way we usually use them) – Jonas Wilms Oct 29 '21 at 18:06
  • so when I do let num = 5; num is in fact... a string value in and of itself??? I thought identifers were not values but more like aliases that the compiler interprets as the value it was assigned to, sort of like a placeholder – Mysasas Oct 29 '21 at 18:10
  • Yes, but the same holds true for object keys. When you do `a.b` the compiler might already know where that value lies in memory and replace a dynamic lookup with the direct address in memory. Both object keys and variable names are however something which we can also refer to dynamically using string values, in which case at runtime the value has to be determined. Usually we do not want to refer to variables dynamically though. – Jonas Wilms Oct 29 '21 at 18:15
  • "*objects have keys associated with them that are their own variables.*" "*Key variable associated with variable*" - I think there is some confusion on the distinction between a variable and a value. Keys are not variables. Or was this just a mistake? Then please [edit] your question to fix the mixup. – Bergi Oct 30 '21 at 04:29
  • fixed it, so it is safe to assume they are value literals? the syntax where they appear to be identifiers is what confused me – Mysasas Oct 30 '21 at 18:15
  • @Mysasas They are string values - sequences of characters in memory. It doesn't matter if those values were created by evaluating a string literal expression, constructed using operators (`+`) or methods (`.slice()`) or received as program input. – Bergi Oct 30 '21 at 19:15

1 Answers1

4

A key is a runtime value whereas an identifier is a syntactical element.

Objects in JS do not have a fixed shape, you can create, update and delete properties in them at will. And you can do so dynamically, without knowing the names of the properties when you write the program, using bracket notation: obj[prop] = val. And similarly you can loop over the keys of an object with for (const prop in obj). This is why keys are strings (or symbols) - you need to access them with/as string values.

The term "identifier" refers to specific syntax in the grammar of the language. It is written by the programmer and parsed by the compiler. Notice that in the expression obj.prop, both "obj" and "prop" are identifiers, so they don't even have to refer to variables. They are just the subset of strings you can write to name variables and properties with dot syntax. They are not values you can access from a javascript program, and don't really exist at runtime any longer.

You might have meant variables though. Variables are distinguished by identifiers, the same identifier in the same scope will refer to the same variable. This distinction happens by name, treating the variable names as strings in the compiler. You just never get in contact with those strings from JS code. When the code is executed, for each declared name a variable with a value will be created in an environment structure for that scope.

So there is some similarity:

  • An object consists of property keys associated with property values.
  • A scope environment consists of variable names associated with variable values.

And actually, this similarity is (or has been) exploited by usage of eval and with statements and the global object, that actually allow the dynamic creation and assignment of variables by name. For these, the interpreter has to keep the variable names (as strings) in memory. It's inefficient and despised though. When they (both object keys and variable names) are not dynamic, the interpreter will be able to optimise them a lot.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Your explanation does make sense, I suppose the way keys in objects do not require quotes is what got me confused between them and identifiers since identifiers do not use quotes. But thanks for the explanation! – Mysasas Oct 30 '21 at 18:18