创建对象的三种方式

  1. 单体模式
   var Teacher = {
       name = "mingzi",
       age = 18,
       showName:function()
       return this.name;
   }
   Teacher.showName();
  1. 原型模式属性放在构造函数里,方法放在原型上
   function Teacher(name,age){
      this.name = name;
      this.age = age;
   }
   Teacher.prototype.showName = function(){
       return this.name;
   }

   var mingzi = new Teacher("小明",18);
   mingzi.showName();
  1. 伪类模式
   class Teacher{
       constructor(name,age){
           this.name = name;
           this.age = age;
       }
       showName(){
           return name;
       }
   }

   var mingzi = new Teacher("小明",age);
   mingzi.showName;
最后修改:2019 年 12 月 10 日 07 : 47 PM
如果觉得我的文章对你有用,请随意赞赏