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

复制代码 代码如下:

  //首先创建父类

  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方法调用中第二个参数开始为传递的数据参数