How does Javascript know what type a variable is?

后端 未结 4 602
遇见更好的自我
遇见更好的自我 2020-12-06 13:52

I don\'t know why I never asked myself that questioned the last years before, but suddenly I could not find any answer for myself or with google.

Javascript is known

4条回答
  •  臣服心动
    2020-12-06 14:31

    The only useful line I can find in the ES5 spec is this:

    Within this specification, the notation “Type(x)” is used as shorthand for “the type of x” where “type” refers to the ECMAScript language and specification types defined in this clause.

    I assume that when the runtime needs to perform an operation that needs to know the type of some value, it will check that value against the grammar defined in the spec for each type, until it finds a match.

    For example, the grammer for a boolean literal is as follows:

    BooleanLiteral ::

      true 
      false
    

    If the value is exactly true or exactly false (e.g. with no quotes) then that value is of type boolean.

提交回复
热议问题