Valid property names, property assignment and access in JavaScript

前端 未结 2 738
旧时难觅i
旧时难觅i 2021-01-19 01:26

Updated Question

What, exactly, qualifies as a valid property name in Javascript? How do various methods of property assignment differ? And how does the property n

2条回答
  •  梦毁少年i
    2021-01-19 02:21

    Short Answer

    Object property names can be any valid identifier, numeric literal, or string literal (including the empty string).

    With that said, there are some potentially confusing intricacies to keep in mind about JavaScript property names, as described below.

    And unless you're working with valid (non-negative integer) array indexes, it's a good idea to explicitly assign all numerical property names as strings.

    Negative Numbers

    What might look like a negative number is actually an expression — something property names do not support.

    // SyntaxError
    const obj = { -12: 'nope' };
    

    Fortunately, bracket notation handles expressions for us.

    // Successful property assignment.
    const obj = {};
    obj[-12] = 'yup';
    

    Typecasting

    All property names are typecasted into strings before being stored.

    const obj = {
      12: '12'
    };
    
    console.log(typeof Object.keys(obj)[0]); // -> string
    

    Parsing

    But even before typecasting occurs, keys are parsed according to the syntax used, and transformed into a decimal literal.

    const obj = {
      // Valid string literal
      '022': '022',
    
      // Interpreted as decimal
      6: '6',
    
      // Interpreted as floating-point
      .345: '0.345',
    
      // Interpreted as floating-point
      1.000: '1',
    
      // Interpreted as floating-point
      8.9890: '8.989',
    
      // Interpreted as decimal
      000888: '888',
    
      // Interpreted as octal
      0777: '511',
    
      // Interpreted as hexadecimal
      0x00111: '273',
    
      // Interpreted as binary
      0b0011: '3',
    };
    
    
    /* Quoted property name */
    console.log(obj['022']); // "022"; as expected
    console.log(obj[022]); // undefined; 022 is an octal literal that evaluates to 18 before our lookup ever occurs
    
    /* Valid (non-negative integer) array index */
    console.log(obj[6]); // "6"; as expected
    console.log(obj['6']); // "6"; as expected
    
    /* Non-valid array index */
    console.log(obj[0x00111]); // "273"; we're accessing the property name as it was assigned (before it was parsed and typecasted)
    console.log(obj['0x00111']); // undefined; after parsing and typecasting, our property name seems to have disappeared
    console.log(obj['273']); // "273"; there it is, we found it using the evaluation of our original assignment
    

提交回复
热议问题