js basics---object

1. The object is composed of attributes and methods.
2. Creation method:

  • Use literals to create objects
  • Use new Object to create objects
  • Using the constructor to create an object

1 literal

<script>
   //1.创建对象
    var dog = {
    
    
            name: '可可',
            type: '阿拉斯加犬',
            age: 5,
            color: 'brown',
            bark: function() {
    
    
                console.log('汪汪汪');
            },
            showFilm: function() {
    
    
                console.log('导盲犬');
            }
        }
    //2访问
    //(1)访问属性
    console.log(dog.name);
    console.log(dog['age']);
    //(2)访问方法
    dog.bark();
    dog.showFilm();
</script>

2 new Object

<script>
        //1.创建对象
        var person = new Object();
        person.name = '鸣人';
        person.sex = '男';
        person.age = 19;
        person.showSkill = function() {
    
    
            console.log('影分身术');
        };

        //2访问
        //(1)访问属性
        console.log(person.name);
        console.log(person['age']);
        //(2)访问方法
        person.showSkill();
    </script>

3 Constructor

Extract some public attributes and methods from the object and encapsulate them into functions

<script>
        //构造函数
        function Hero(name, type, blood, attack) {
    
    
            this.name = name;
            this.type = type;
            this.blood = blood;
            this.attack = attack;

            this.introduce = function() {
    
    
                console.log(this.name + this.type + this.blood + this.attack);
            }
        }
        //创建对象
        var hero1 = new Hero('廉颇', '力量型', '500血量', '近战');
        var hero2 = new Hero('后羿', '射手型', '100血量', '远程');
        //访问属性
        console.log(hero1.name);
        console.log(hero1['type']);
        //访问方法
        hero1.introduce();
    </script>

note:

  1. The constructor convention is to capitalize the first letter.
  2. You need to add this before the properties and methods in the function to indicate the properties and methods of the current object.
  3. There is no need to return in the constructor to return the result.
  4. When we create an object, we must use new to call the constructor.

The role of new:

  1. Create a new empty object in memory.
  2. Let this point to this new object.
  3. Execute the code in the constructor to add properties and methods to this new object.
  4. Return this new object (so there is no need to return in the constructor).

Traverse the properties of the object:

for (var k in obj) {
     console.log(k); // 这里的 k 是属性名 
     console.log(obj[k]); // 这里的 obj[k] 是属性值 
}

E.g:

<script>
    var star = {
    
    
        name: '小新',
        age: 18,
        sex: '男',
        sayHi: function() {
    
    
            alert('大家好啊~');
        }
    };
    for (key in star) {
    
    
        console.log(key); //属性
        console.log(star[key]); //属性值
    }
</script>

Insert picture description here

Guess you like

Origin blog.csdn.net/pilgrim_121/article/details/113040640