编程语言
首页 > 编程语言> > JavaScript——自定义类或对象

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();

 

标签:function,name,自定义,对象,age,JavaScript,Student,student1
来源: https://www.cnblogs.com/remix777/p/15306970.html