正确的方法来定义构造函数

时间:2016-04-08 04:29:28

标签: javascript oop constructor

我正在尝试遵循MDN文章面向对象JavaScript简介。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript

在那里它将构造函数定义为:

var Person = function(firstName) {
  this.firstName = firstName;
}

function Student(firstName, subject) {
  // Call the parent constructor, making sure (using Function#call)
  // that "this" is set correctly during the call
  Person.call(this, firstName);

  // Initialize our Student-specific properties
  this.subject = subject;
}

为什么Student构造函数未定义为:

var Student = function(firstName, subject) {
  // Call the parent constructor, making sure (using Function#call)
  // that "this" is set correctly during the call
  Person.call(this, firstName);

  // Initialize our Student-specific properties
  this.subject = subject;
}

0 个答案:

没有答案