Javascript Programming: Is the period [.] Always an operator?

JavaScript has a point . always an operator?

For instance:

 AnObject.aMethod() 

Are there any examples when the point is . not an operator?

+4
source share
4 answers

No.

  • "Strings."
  • /regular.expressions/
  • 1.2 // Numbers
  • // Comments.
+9
source

This is not an operator in numeric literals.

 var x = 12.5; 
+5
source

Although to describe member access . as an operator is general, I think this is somewhat wrong in languages ​​like Java, Javascript, C or C ++.

Other binary operators have an expression on the left and an expression on the right, while the element access operator does not allow the expression on the right, but just the field identifier ... i.e. quite specific syntactic form.

For example, for other binary operators it makes sense to talk about left or right associativity (i.e. if a op b op c - a op (b op c) or (a op b) op c ), although this is a meaningless question about access to member, since only one of the two forms is syntactically valid (you cannot even write a.(b) ). The same goes for priority.

If we are not talking about operators in terms of associativity or priority, but only in terms of the character in the expression (i.e. the question is that the dot character in the expression does not mean access to the member), then obviously you have a floating a comma (where it plays the role of a decimal point), a dot character in string literals (he plays ... the role of one dot character) and a dot character in regular expressions (where it means either itself again, or "any character" depending on whether it was shielded or not, respectively).

In addition, as Quentin remembered me, you can have points in the comments where the meaning is left for human interpretation.

+5
source

Besides the ones already specified (for example, numbers, strings, and regular expressions), punctuation periods may not always work. Thus, in such cases, when the property you are trying to access has a special character,. will not be a valid statement:

 var obj = { 'prop1' : null, 'prop-2' : null }; obj.prop1;//it works obj.prop-2;// it doesn't work. you should access with via the brackets operator : obj['prop-2']; 

My point was that there are times when a point is not always a valid operator (even if you think it will).

+1
source

Source: https://habr.com/ru/post/1390779/


All Articles