JavaScript中创建默认对象的方式

    <script>
        //方式一
        let obj1 = new Object();
        obj1.name = "zs";
        obj1.age = 18;
        obj1.gender = "male";
        obj1.say = function() {
            console.log(this.name + ": Hello!");
        };
        obj1.say();

        //方式二
        let obj2 = {};
        obj2.name = "ls";
        obj2.age = 19;
        obj2.gender = "male";
        obj2.say = function () {
            console.log(this.name + ": Hello!");
        };
        obj2.say();

        //方式三
        let obj3 = {
            name: "ww",
            age: 20,
            gender: "male",
            say : function () {
                console.log(this.name + ": Hello!");
            }
        };
        obj3.say();
    </script>
(0)

相关推荐