object

1. Create an object

    1.1 Literal creation method

1 var box={
2         name:'小明',
3         age:20,
4         qq:23435235,
5     };
6         alert(box.qq)
7         alert(box['age'])

    1.2 new creation

            var box=new Object()
            box.name ='Akari' ;
            box.age=20;
            box.qq=123421545;
            alert(box.name)
            alert(box['age'])

2. Operation of properties in objects

  2.1 Query properties

            var box=new Object()
            box.name ='Akari' ;
            box.age=20;
            box.qq=123421545;
            box.qq     // Get the property qq 
            box.age    // Get the property age

 

  2.2 Setting properties

            var box = {
                name: 'Akari' ,
                age:20,
                qq:24253241,
            };
            box.qq =4641451;       // Change the qq property value 
            box.wechat='Learning is boundless'; // Create a new property and assign a value

  2.3 Deleting attributes

            var box = {
                name: 'Akari' ,
                age:20,
                qq:24253241,
            };
            delete box.name   // delete property name 
            delete box.age    // delete property age

 

  2.4 Detecting attributes (judging whether an attribute exists in an object)

    2.4.1 in operator detection

            var box = {
                name: 'Akari' ,
                age:20,
                qq:24253241,
            };
            alert('name' in box)//true
            alert(name in box)  //false

    2.4.2  hasOwnProperty detection

            var box = {
                name: 'Akari' ,
                age:20,
                qq:24253241,
            };
            alert(box.hasOwnProperty('name'))//true
            alert(box.hasOwnProperty(name))  //false

    2.4.3! == undefined detection

            var box = {
                name: 'Akari' ,
                age:20,
                qq:24253241,
            };
            alert(box.name!==undefined)    //true
            alert(box.weixin!==undefined)  //false

 

2.5 Enumerating properties (traversing the properties of the object)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325109200&siteId=291194637