JS foundation # day 5

catalogue

1, Constructor to create an object

2, Basic and reference data types

3, Literal creation object

1, Constructor to create an object

1. The function called with the new keyword is the constructor, which is a function specially used to create objects.

    // 1. When typeof is used to check an object, object will be returned
    var obj = new Object();
    console.log(typeof obj);

    // 2. The value saved in the object is called an attribute
    // Add attribute syntax to object: object Attribute name = attribute value;
    obj.name = "name of a fictitious monkey with supernatural powers";
    obj.gender = "male";
    obj.age = 18;
    console.log(obj); // {name: "Wukong", gender: "male", age: 18}
    
    // 3. Attribute syntax in query object: object Attribute name
    console.log(obj.name); // name of a fictitious monkey with supernatural powers
    // If there is no attribute in the query object, no error will be reported, but undefined will be returned
    console.log(obj.hello); // undefined
    
    
    // 4. Modify attribute value syntax: object Attribute name = new attribute value
    obj.name = "Bajie";
    console.log(obj); // {name: "Eight Precepts", gender: "male", age: 18}

    // 5. Add an attribute syntax to the object: object Attribute name = attribute value
    obj.tel = "1234566";
    console.log(obj); // {name: "Eight Precepts", gender: "male", age: 18, tel: "1234566"}

    // 6. Delete object attribute syntax: delete object Attribute name
    delete obj.name; // {gender: "male", age: 18, tel: "1234566"}
    console.log(obj);

2. Attribute name and attribute value

(1) Attribute name

    // The attribute name of an object is not required to comply with the specification of the identifier, but try to follow the specification of the identifier
    // obj.var = "hello";
    console.log(obj); // {name: "Eight Precepts", var: "hello"}
    
    // If you want to use a special property name, you cannot use The way to operate
    // Another method needs to be used, syntax. This method is also needed when reading object ["attribute name"] = attribute value
    obj["123"] = "7685";
    console.log(obj["123"]); // 7685
    
    // Using [] to manipulate attributes is more flexible,
    // A variable can be passed directly in [], so that the value of the variable will read the attribute
    obj["nihao"] = "Hello";
    var n = "nihao";
    console.log(obj[n]); // Hello

(2) Attribute value

    // The attribute value of JS object can be any data type
    obj.test = true;
    obj.test = null;
    obj.test = undefined;

    // It can even be an object
    // Create an object
    var obj2 = new Object();
    obj2.name = "you 're incompetent";
    // Assign obj2 as an attribute value to obj
    obj.test = obj2;
    console.log(obj); // {123: "7685", name: "Bajie", nihao: "hello", test: {...}}
    // Get the properties of obj2
    console.log(obj.test.name); // you 're incompetent
    
    // in: operator, through which you can check whether an object contains the specified attribute. If yes, it returns true, and if not, it returns false
    // Syntax: attribute name in object
    // Check whether obj contains the attribute test
    console.log("test" in obj); // true
    // Check whether the obj contains the attribute test1
    console.log("test1" in obj); // false

2, Basic and reference data types

1. Basic data type: the value of the basic data type is directly stored in the stack memory. The value and value exist independently. Modifying one variable will not affect other variables.

Example:

    var a = 123;
    var b = a;
    a++;
    console.log(a); // 124
    console.log(b); // 123

 2. Reference data type: object

Objects are saved in heap memory. Every time a new object is created, a new space will be opened up in heap memory, and variables save the memory address (object reference) of the object. If two variables save the same object reference, the other will also be affected when modifying properties through one variable.

Example:

    var obj = new Object();
    obj.name = "you 're incompetent";
    var obj2 = obj;
    obj.name = "name of a fictitious monkey with supernatural powers";
    console.log(obj.name); // {name: "Wukong"}
    console.log(obj2.name); // {name: "Wukong"}

However, if obj2 is set to null, obj2 will be removed directly and will not affect obj

    obj2 = null;
    console.log(obj); // {name: "Wukong"}
    console.log(obj2); // null

When comparing the values of two basic data types, it is the comparison value. When comparing two reference data types, it is the memory address of the compared object. If the two objects are similar, but the addresses are different, it will also return false.

    var obj3 = new Object();
    var obj4 = new Object();
    obj3.name = "Bajie";
    obj4.name = "Bajie";
    console.log(obj3 == obj4); // false

3, Literal creation object

  • Create an object using object literals. Syntax: {attribute name: attribute value, attribute name: attribute value...}. The attribute name of an object literal can be quoted or not. It is recommended not to. If you want to use some special names, you must use quotation marks.
  • Attribute names and attribute values are a group by group name value pair structure. Names and values are used: connected. Multiple name value pairs are used to separate. If there are no other attributes after an attribute, do not write.

Example:

      var obj = {
      name: "Bajie",
      age: 18,
      gender: "male",
      test:{
        name: "buddhist monk"
      } 
    }
    console.log(obj); // {name: "Eight Precepts", age: 18, gender: "male", test: {...}}

    // 1. Query
    console.log(obj.name); // Bajie
    // 2. Modification
    obj.name = "name of a fictitious monkey with supernatural powers";
    // 3. Increase
    obj.tel = "15519552535";
    // 4. Delete
    delete obj.gender;
    console.log(obj); // {name: "Wukong", age: 18, test: {...}, tel: "15519552535"}
    

Keywords: Javascript

Added by scottd on Thu, 30 Dec 2021 09:40:02 +0200