JS高级-----------------创建对象的三种方式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<script>

    //    创建对象的三种方式
    //    1.字面量的方式
    var per = {
        name: "小明",
        age: 18,
        sex: "",
        eat: function () {
            console.log(this.name + "吃早饭")
        }
    };
    per.eat();
    //    2.调用系统的构造函数
    var per1 = new Object();
    per1.name = "小红";
    per1.age = 19;
    per1.eat = function () {
      console.log(this.name + "在吃午饭");
    };
    per1.eat();
    //    3.自定义构造函数
    function Person(name, age, sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.eat = function () {
            return this.name + "在吃晚饭";
        }
    }

    var per3 = new Person("小亮", 20, "不详");
    console.log(per3.eat());

</script>
</body>
</html>

 

posted @ 2018-08-23 06:56  {颜逸}  阅读(142)  评论(0编辑  收藏  举报