Last time we briefly covered the basics of basic datatypes, this time we’ll focus on the basics of referring to object in datatypes

A, definitions,

  • 1. Use key:value pairs (commonly known as attribute names and attribute values) to describe the characteristics of an object (every object is a complex, there are zero to multiple groups of key-value pairs);
  • 2, {key: value,… } Each group of key-value pairs is in the format of key: value, separated by commas;
  • 3. Key cannot be a reference data type and value can be any data type
    let obj = {
    	name: 'Joe'.age: 25.sex: 'male'.score: [100.98.89].fn: function () {}};console.log(obj);
    Copy the code

Since our objects are mostly key-value pairs, let’s talk about key-value pairs

Key value pairs

  • Composition:
    • Attribute name: Attribute value
  • Operation mode:
    • Property name = property value
    • Object [attribute name] = attribute value

    ‘dot’ and ‘[]’ can be understood here as’ of ‘

1, obtain

  • Get the value:
    • 1, object. Property name
      • Based on this method, the property name is.At the back of the
      • This way, the attribute name cannot be a number
    • 2. Object [attribute name]
      • 1. To operate in this way, ensure that the attribute name is a value (string/number/Boolean).
      • 2. If it is a variable instead of a value, it operates on the value stored in the variable as the property name of the object
      • This method can only be used if the attribute name is a number

    If the specified attribute does not exist, the obtained attribute value is undefined (no error)

    let n = 100;
    let obj = {
    	1: 100
    };
    console.log(obj[1]);
    console.log(obj1.); //=>Uncaught SyntaxError: missing ) after argument list The point-based approach has its own limitations, property names cannot be numeric, cannot be objects. Numeric properties, only objects can be usedconsole.log(obj[1]);
    console.log(obj['1']); //=> Other non-string formats as attribute names are no different from string formats
    
    obj.n = 200; //=> {n:200} n is the attribute name (data format is string)
    obj['n'] = 200; //=> {n:200} same as above
    
    obj[n] = 200; //=> {100:200} => obj[100]=200   N is itself a variable (n and'n'Is a variable, is a string value), which represents the stored value100(is a number format) obj[m] =200; //=>Uncaught ReferenceError: m is not defined The variable m is not defined obj[true] = 300; //=>{true:300}
    obj[undefined] = 400; //=>{undefined:400}
    
    console.log(obj);
    Copy the code
  • Keys (Object) : Returns an array containing all property names
    let obj = {
        sex: 0
    };
    / / = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    1Gets the property value of the specified property nameconsole.log(obj.sex); / / = > 0
        console.log(obj['sex']); / / = > 0
    2If the specified attribute does not exist, the obtained attribute value isundefined(No error)console.log(obj['age']); //=>undefined
    3Gets all property names in the current object: returns an array containing all property namesconsole.log(Object.keys(obj)); //=>["sex"]
    Copy the code

2, increase | modified

  • Principle: Object attribute names (keys) are not allowed to be duplicated

    • This property is new if it did not exist before
    • If the name of the property is present, the value of the property is changed
    let obj = {
        sex: 0
    };
    / / = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    obj.name = 'Joe';/ / = > new
    obj['name'] = "Bill";//=> modify Because name: 'Zhang SAN' already exists in obj, so this operation is modified
    Copy the code

3, delete,

  • True delete: Removes the property from the object completely
    • delete obj.name
  • Fake delete: The current property still exists, but the property value is empty
    • obj.name = null

Reference data types cannot be used as attribute names

let n = {
    x: 100
};
let m = [100.200];
let obj = {};
obj[n] = "Zhang"; //=>obj[{x:100}] =>{"[object object]":" Zhang Three "}
obj[m] = "Bill"; //=>obj[[100,200]] =>{"100,200":" "}
console.log(obj); 
    
//=> If the attribute name must be an object, it can only be handled based on the new data structure Map in ES6
Copy the code

In order to deepen our understanding, we quote three questions and explain them in detail

4. The characteristics of arrays in relation to objects

Arrays are special objects

  • 1. Its attribute name is a number. The number starts from zero and increases gradually, and each number represents the position of the current item.
  • 2. The default length property stores the length of the array
let arr = [10.20.30];
console.log(arr[0], arr[1], arr[2]);
console.log(arr.length);
console.log(arr['length']);
Copy the code

Mind mapping