0

Could somebody explain me what Crockford means when he speaks about string constants:

"The dot notation can be used when the subscript is a string constant in the form of a legal identifier."

http://javascript.crockford.com/survey.html

AFAIK he refers to the constants as the object properties but why called constants if their value may change at any time.

Or, does he refer at those as constants, because once the property name is defined, it never changes?

If I should make a pseudo-code base on his words, I'll do something like this (note that this code is just for clarify my idea and it is not actually javascript code, despite of the sameness):

const const1 = 'name'; object person = {name = 'Ed'}; print person.const1 //it should print "Ed" 

Thank u very much

5
  • 1
    With numbers or expressions you can not use dot notation. You use square brackets instead. Commented Oct 2, 2015 at 1:48
  • If const1 is not a property of the person Object that won't print 'Ed'. Also, please refrain from the use of pseudo-code. Commented Oct 2, 2015 at 1:50
  • I'm not aware of the use of constants in javascript, that's why I opted for use a pseudocode-like encoding. It is quite confusing to me such text: when subscript is a string constant Commented Oct 2, 2015 at 2:03
  • 1
    He's using the word "constant" colloquially here. He simply means "a particular fixed sequence of characters". This is completely different from a const string, which in spite of being called const, is still a variable, and which would be used as in person[const]. Remember that when Crockford wrote this JavaScript did not even have a const variable type. Commented Oct 2, 2015 at 4:21
  • @torazaburo thank you very much. It now makes sense to me. I struggle a lot with this kind of "colloquial" uses. Really appreciated your comment. Commented Oct 2, 2015 at 17:32

5 Answers 5

1

"The dot notation can be used when the subscript is a string constant in the form of a legal identifier."

This means that a string value which is constant in the sense of unchanged can be used to read and write the same property value. The message is that although any string value can be used within bracket notation, it must be a legal identifier to be written using dot notation.

For example someObject["propertyName"] means the same as someObject.propertyName using dot notation. Both are valid and work. However, while someObject["too....many...dots"] is valid and works using bracket notation, someObject.too....many...dots is not a legal identifier and doesn't work.


In reply to comment:

  1. Consider "constant string" as the name used by a programmer to access a property value: it is fixed at the time of writing and does not change when the code is run.

  2. Property names using dot notation do not use quotes. object."property" is always illegal.

  3. Object property names and variable names can have the same names but don't refer to the same thing.

    x; // look for variable x in function scope. obj.x; // look for property x of object obj var x = obj.x; // transfer the value of obj.x to variable x 
  4. The property name in bracket notation is the string value within the brackets. If this is a string literal it will be quoted. If it not a string literal it will be evaluated as a javascript expression with the result used as property name.

    obj.color = "red"; var prop = "color" obj[ prop]; // value of property specified by the expression (prop) is "red" 
Sign up to request clarification or add additional context in comments.

1 Comment

This part constant in the sense of unchanged is main thing; I guess that if he had write "The dot notation can be used when the subscript is a string in the form of a legal identifier.", without the word constant, it will mean that using any string literal (using quotes) or string variables (var x = 'property') that is a legal identifier would work: object."property" = 3; or even var x = "property"; object.x = 3; //making reference to object.property instead of object.x
1

Crockford is using the word "constant" colloquially here. He simply means "a particular fixed sequence of characters". This is completely different from an ES6 const string, which in spite of being called const, is still an (immutable) variable, and which would of course, like any other variable holding a key, be used as in person[const]. Remember that when Crockford wrote this JavaScript did not even have a const variable typej, obviously that is not what he is referring to.

Comments

1

Strings in JavaScript are primitive inmutable values, that's why he uses the word constant, and he is talking about you can use dot notation on objects when the property identifier is a legal (word) string value.

You are just mixing two different concepts.

The example from @Traktor53 is a good way to explains what Crockford means.

Comments

0
  • object.hello OK.
  • object.2 fail because 2 is not a valid identifier.
  • object.hello world fail because of space,
  • object.a-b what do you think?

2 Comments

Hi @Jaromanda X, I understand that, but, why he refers as string constant to, taking for instance your example, hello, from object.hello.
if you understand the answer, and have not learned anything new from it, then perhaps you've asked the wrong question
0

Here's what I believe

var obj = {'You can have spaces here' : 'value 1', cantHaveSpacesHere: 'value 2'}; console.log(obj['You can have spaces here']); console.log(obj.cantHaveSpacesHere); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.