Javascript 面向对象编程(二)

创建对象三种方式

    1  字面量的方式

var per1={
      name:"沐风",
      age:20,
      sex:"男",
      eat:function () {
        console.log("吃");
      },
      readBook:function () {
        console.log("");
      }
    };

    2  调用系统的构造函数

 var per2=new Object();
    per2.name="沐风";
    per2.age=30;
    per2.sex="男";
    per2.eat=function () {
      console.log("吃");
    };
    per2.play=function () {
      console.log("玩");
    };

    3  自定义构造函数方式

function Person(name,age,sex) {
      this.name=name;
      this.age=age;
      this.sex=sex;
      this.play=function () {
        console.log("天天打游戏");
      };
    }
    var per=new Person("沐风",18,"男");
    console.log(per instanceof Person);

猜你喜欢

转载自www.cnblogs.com/cnki/p/9656772.html