Forty-six - fifty-one

Forty-six, JavaScript - object

1. Object

  • type of data:

                original value :

                    1. Value Number

                    2. Big Integer

                    3. String String

                    4. Boolean Boolean

                    5. Null

                    6. Undefined

                    7. Symbol Symbol

            In addition to the seven primitive values, the data types used later are collectively referred to as objects


 

  • Primitive values ​​can only be used to represent some simple data, such as gender, age, and cannot represent complex data

  • object         

                - Object is a composite data type in JS

                    It is equivalent to a container, in which different types of data can be stored,

                        For example, the object can store the person's name, height, age

  • Multiple types of data can be stored in an object
  1. The values ​​stored in the object, we call them properties
  2. Create an object (new can be omitted)

                   let objectName = new Object

  3. Add properties to objects

                    object.property name = property value

  4. Read a property in an object

                    object.PropertyName

                        - if reading a property that is not present in the object

                            It will not report an error but report undefined

  5. Modify a property in the object: naming the new one will result in an overwrite

                   object.PropertyName = new property value

  6. delete attribute

                   delete object.property name                      

 <script>
        /*
            数据类型:
                原始值:
                    1. 数值  Number
                    2. 大整数 BigInt
                    3. 字符串 String
                    4. 布尔值 Boolean
                    5. 空值 Null
                    6. 未定义 Undefinded
                    7. 符号 Symbol

            除了七种原始值之外,后面所用到的数据类型,都统称为对象


            原始值只能用来表示一些简单数据,如性别,年龄,不能表示复杂数据

            对象
                - 对象事JS中一种复合数据类型
                    它相当于一个容器,在对象中可以存储不同的类型数据,
                        例如对象中可以存储人的姓名、身高、年龄
        
        */

    

        /*
            对象中可以存储多个各种类型的数据
                对象中存储的数值,我们称之为属性

            创建一个对象(new可省略)
                let 对象名  = new Object

            向对象中添加属性
                对象.属性名 = 属性值


            读取对象中的某个属性
                对象.属性名
                    - 如果读取的是对象中没有的属性
                        不会报错而是报undefinded

            修改对象中的某个属性:命名新的,会产生覆盖
                对象.属性名 = 新的属性值
            
            删除属性
                delete 对象.属性名
            
        */

            // 创建对象
        let obj = new Object()

        obj.name = "张三"
        obj.age = 10
        obj.gender = "男"

        console.log(obj)

        //读取对象中的某个属性
        console.log(obj.name)

        //修改属性
        obj.name = "李四"

        //删除属性
        delete obj.name

        console.log(obj.name)


    </script>

 Forty-seven, JavaScript - the properties of the object

1. Attributes

  • let property name = new Object()

            attribute name  

                - Usually the attribute name is a string, so the attribute name can be any value, there is no special requirement

                    But if the obtained name is too special to be used directly, it needs to be wrapped with []

                    Even so, we strongly recommend that attribute names also follow the identifier convention

               

  • You can also use symbols as attribute names to add attributes

                    When obtaining this property, Symbol must also be used

                - Attributes added using Symbol, usually those attributes that do not want to be accessed by the outside world

  • - When using [] to operate attributes, you can use variables, variables in square brackets do not need to add quotation marks

  • When checking an object with typeof, return object

  • in operator

                    - Used to detect whether an object contains an attribute

                    - syntax property name in object name

                    - Return true if there is one, false if not

  • Add attributes using symbols as attribute names

                    When obtaining this property, Symbol must also be used

                - Attributes added using Symbol, usually those attributes that do not want to be accessed by the outside world

 

<script>

        /*
            let 属性名 = new Object()

            属性名  
                - 通常属性名就是一个字符串,所以属性名可以实任何值,没有什么特殊要求
                    但是如果取得名字太特殊了,不能直接使用,需要用[]来包裹
                    虽然如此,但是我们还是强烈建议属性名也按照标识符规范命名

                
                - 也可以使用符号(symbol)作为属性名,来添加属性
                    获取这种属性时,也必须使用Symbol
                - 使用Symbol添加的属性,通常是那些不希望呗外界访问的属性

        */

       let obj = new Object()

       console.log(obj)

       //使用symbol作为属性名,得到Symbol对象
       let mySymbol = Symbol()

       //使用symbol作为属性名
       obj[mySymbol] = "通过symbol添加的属性"

       console.log(obj[mySymbol])



    </script>
  • - When using [] to operate attributes, you can use variables, variables in square brackets do not need to add quotation marks 

 

<script>

        /*
            let 属性名 = new Object()

            属性名  
                - 通常属性名就是一个字符串,所以属性名可以实任何值,没有什么特殊要求
                    但是如果取得名字太特殊了,不能直接使用,需要用[]来包裹
                    虽然如此,但是我们还是强烈建议属性名也按照标识符规范命名

                
                - 也可以使用符号(symbol)作为属性名,来添加属性
                    获取这种属性时,也必须使用Symbol
                - 使用Symbol添加的属性,通常是那些不希望呗外界访问的属性

                - 使用[]去操作属性时,可以使用变量,中括号中的变量不需要添加引号

            属性值
                - 对象的属性值可以是任意得而数据类型,也可以是一个对象

        */

    //    let obj = new Object()

    //    console.log(obj)

    //    //使用symbol作为属性名,得到Symbol对象
    //    let mySymbol = Symbol()

    //    //使用symbol作为属性名
    //    obj[mySymbol] = "通过symbol添加的属性"

    //    console.log(obj[mySymbol])

    
    let obj = new Object()

    let str = "address"

    obj[str] = "南京"

    console.log(obj)

    </script>
  • in operator

                - Used to detect whether an object contains an attribute

                - syntax property name in object name

                - Return true if there is one, false if not

 console.log("name" in obj)

 Forty-eight, JavaScript - the literal value of the object

1. Literal {}

  •    object literal
  1.  Objects can be created using {}
  2. Use {} to create objects, you can add objects directly to properties
  3. grammar:

                    {

                        Attribute name: attribute value,

                        ["property name"]: property value,

                    }

  •  Regarding ["attribute name"]: attribute value, whether the attribute value should be enclosed in double quotation marks
  • If it is a directly named attribute value, double quotes are required , for example, the attribute name is ["age"]: attribute value
  • When referencing a created object, do not use double quotes              

                 //Use symbol as the attribute name to get the Symbol object
                       let mySymbol = Symbol()

               //Use symbol as the attribute name
                       obj[ mySymbol ] = "Attribute added by symbol"


    <script>

    /*

        对象字面量
            - 可以使用{} 来创建对象
                语法:let 属性名 = {}
            - 使用{}创建对象,可以直接向属性中添加对象
            - 语法:
                {
                    属性名:属性值,
                    ["属性名"]: 属性值,
                }

    */



    //使用{} 来创建对象
    let objs = {}

    //使用{}创建对象,可以直接向属性中添加对象
    let objec = {
        name: "张三",

        ["age"]: 18,
    }

    console.log(objec)

    </script>

Forty-nine, JavaScript - enumerate the properties in the object

1. Enumerated attributes

  • Enumerate attributes: get all the attributes in the object

            for-in statement

            - grammar

                for(let propName in object){

                    statement. . .

                    console . log ( proName , obj [ proName ] )

                    console.log(property name, property value)

                }

            - The for-in loop body is executed multiple times, and several attributes will be executed several times

                Each execution will assign a property name to the variable we defined

  •  Notice:

                - Not all attributes can be enumerated, for example: attributes added using symbols cannot be enumerated [Symbol()]

 

  <script>
        /*
            枚举属性:将对象中属性全部获取

            for-in语句
            - 语法
                for(let propName in 对象){
                    语句。。。

                    console.log(proName,obj[proName])
                    console.log(属性名,属性值)
                }

            - for-in的循环体会执行多次,有几个属性会执行几次
                每次执行时都会将一个属性名赋值给我们定义的变量

            注意:
                - 并不是所有的属性都可以枚举,比如:使用符号添加的属性不能枚举[Symbol()]



        */

        let obj = {
            name: "张三",
            age: 11,
            gender: "男",
           // [Symbol()]:"测试的属性" //符号添加的属性不能枚举

        }

        for(let propName in obj){

            //枚举属性名,属性值
            console.log(propName,obj[propName])
        }

    </script>

Fifty, JavaScript - variable type

1. Variable type

  • Definition: variable type value means that the attribute name and attribute value in the object can be changed

  • When creating an object, the attributes and attribute values ​​of the object are also equivalent to the mapping relationship of a table in the memory, and the real attribute value is also stored in a space in the memory, and the memory address is stored in the table
  • The object variable name from new is obj 
  • The corresponding value stores the address of the property of the object
  • The address of the property name and property value in the properties of the object
  • The attribute value address points to the place where the attribute value is actually stored in memory

 

  • Primitive values ​​are immutable types and cannot be modified once created

            In memory, duplicate primitive values ​​are not recreated

  • Object is of mutable type

                    - Variables and memory are stored in memory addresses

                    - Variable type value refers to the attribute name and attribute value in the object can be changed

                Note :

                    - if two variables point to the same object at the same time

                        When an object is modified through a variable, another object will also change (the memory address changes)

                    

  • When comparing two objects for equality or congruence, the memory addresses are compared,

  • When an object is modified, all variables pointing to that object will change

  • When comparing two objects for equality or congruence, the memory addresses are compared 

 

   <script>
        /*
            原始值都属于不可变类型,一旦创建无法修改
            在内存中,不会重复创建重复的原始值


            

        */

    
        /*
            对象属于可变类型
                - 变量和内存中存的都是内存地址
                - 可变类型值指的是对象中的属性名和属性值是可以改变的
            注意:
                - 如果两个变量指向同时指向同一个对象
                    通过一个变量修改对象时,另一个对象也会发生改变(内存地址发生改变)

                - 当对两个对象进行相等或者全等的比较时,比较的是内存地址,
        */

        let obj = new Object()

        obj.name = "张三"

        let obj2 = new Object()



        let obj3 = new Object()

        console.log(obj2 === obj3)

    </script>

Fifty-one, JavaScript - variables and objects

1. Variables and Objects

modify object

                - When modifying an object, if there are other variables pointing to the object

                    Then all variables pointing to the object will be affected

           

            modify variable

                - Modifying variables will only affect the current variable

           

            When using variables to store objects, it is easy to increase the complexity of the code by changing the object pointed to by the variable

                So usually, variables that declare storage objects use const

           Notice:  

                const just prohibits variable assignment, prohibits changing the value, and allows when modifying the object

                Because it is const obj, the variable name and address of obj are fixed and cannot be modified

<script>

        /*
            修改对象
                - 修改对象时,如果有其他变量指向该对象
                    则所有指向对象的变量会受到影响
            
            修改变量
                - 修改变量,只会影响当前的变量
            
            在使用变量存储对象时,很容易因为改变变量指向的对象,提高代码的复杂度
                所以通常情况下,声明存储对象的变量会使用const

            注意:  
                const只是禁止变量赋值,禁止改变量,而修改对象时允许的
                因为是const obj 所以obj的变量名和地址是固定的 额不可修改的
            
            
        */

        let obj = {
            name:"张三",

        }

        let obj2 = obj

        obj2.name = "李四"  // 修改对象
        
        obj = { }  //修改变量。创建了一个新的对象






    </script>

Guess you like

Origin blog.csdn.net/z972065491/article/details/128341795