源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

JavaScript中使用构造函数实现继承的代码

  • 时间:2020-08-31 10:41 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:JavaScript中使用构造函数实现继承的代码
[u]复制代码[/u] 代码如下:
//首先创建父类 function Person(name, age, address) { this.name = name; this.age = age; this.address = address; } //创建子类 function Student(score) { this.score = score; //可以用call方法或者是apply方法调用函数的构造函数 //调用父类的构造函数,通过call方法调用Person类的构造函数。这样就会在student中初始化Person对象,student也就有了Person的属性的副本 Person.call(this,"zhangsan",22,"中国北京!"); } var student = new Student(100); alert(student.address + student.score + "分");
//上述Person.call方法调用中第二个参数开始为传递的数据参数
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部