首页 > 编程语言 > 详细

JavaScript——自定义类或对象

时间:2021-09-21 18:15:28      阅读:18      评论:0      收藏:0      [点我收藏+]

介绍

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

 

JavaScript——自定义类或对象

原文:https://www.cnblogs.com/remix777/p/15306970.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!