Object property name as number

前端 未结 6 695
盖世英雄少女心
盖世英雄少女心 2020-11-22 16:20

According to the official JavaScript documentation you can define object literal property names using integers:

Additionally, you can use a numeric or

6条回答
  •  时光说笑
    2020-11-22 16:59

    Dot notation only works with property names that are valid identifiers. An identifier must start with a letter, $, _ or unicode escape sequence. For all other property names, you must use bracket notation.

    In an object literal, the property name must be an identifier name, string literal or numeric literal (which will be converted to a string since property names must be strings):

    var obj = {1:1, foo:'foo', '+=+':'+=+'};
    
    alert(obj[1] + ' ' + obj.foo + ' ' + obj['+=+']); // 1 foo +=+
    

提交回复
热议问题