JavaScript——自定义类或对象

介绍

  使用function自定义类

 

定义

  JavaScript使用function进行定义类

  类的属性在function中定义

  方法在function之外使用 className.propertype.functionName = functionName 原型方式添加

      this关键字指向调用该方法的对象。所以 let student  = new Student("陈威",22),能够给该对象附加上属性

function Student( name , age ){
 this.name = name;
 this.age = age;
}

Student.prototype.showInfo = function(){
 alert("姓名:"+this.name +",年龄:"+this.age);
}

let student1 = new Student("张三",23);
student1.showInfo();

 

posted @ 2021-09-18 04:18  remix_alone  阅读(60)  评论(0)    收藏  举报