JavaScript 如何将功能放入抽象函数中

How to put functionality into abstract function in JavaScript

我正在努力使 JavaScript 对象中的某些方法可由子对象 class 继承,但我不想让父对象 [=18] =] 被实例化。这是我写的一些代码来说明这一点:

/**
* Shows how basic abstraction works with JavaScript
*/

//Define the person object with a first name, last name, and an age
function Person(firstName, lastName, age) {
    //Make it so that this object cannot be instantiated by identifying its constructor
    if(this.constructor === Person) {
        throw new Error("Can't instantiate an abstract class of type Person!");
    }

    //Assign instance variables
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;

    //Create simple get methods
    this.getName = function(){
         return this.firstName + " " + this.lastName;
    }

    this.getFirstName = function() {
         return this.firstName;
    }

    this.getLastName = function() {
         return this.lastName;
    }

    this.getAge = function() {
         return this.age;
    }
}

//Define the student constructor
function Student(firstName, lastName, age, subject) {
    //Assign the instance variables including the new subject variable
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
    this.subject = subject;

    //Add a new function to get the subject from Student
    this.getSubject = function() {
         return this.subject;
    }
}
Student.prototype = Object.create(Person.prototype);
Student.prototype.constructor = Student;

//Testing the inheritance
var joe = new Student("Joe", "Shmo", 33, "Computer Science");
console.log("First Name: " + joe.getFirstName()); //The getFirstName() function is defined in the superclass
console.log("Subject: " + joe.getSubject()); //The getSubject() function is defined in the subclass

使用此代码,我在尝试调用 Student 对象 joe 上的 getFirstName 时遇到错误。看起来让 getFirstName 可以被 subclass 继承是非常有用的。

我真的希望能够在父 class 中定义 getName 函数,这样我就可以让子 class 继承该功能,例如 Student。有什么办法吗?如果有任何帮助,我将不胜感激!

您需要在 Person 原型中定义您的方法,而不是在 Person 的实例中。这样,当您执行 Object.create(Person.prototype):

时,它们将被复制

/**
* Shows how basic abstraction works with JavaScript
*/

//Define the person object with a first name, last name, and an age
function Person(firstName, lastName, age) {
    //Make it so that this object cannot be instantiated by identifying its constructor
    if(this.constructor === Person) {
        throw new Error("Can't instantiate an abstract class of type Person!");
    }

    //Assign instance variables
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;

}

Person.prototype.getName = function(){
  return this.firstName + " " + this.lastName;
}

Person.prototype.getFirstName = function() {
  return this.firstName;
}

Person.prototype.getLastName = function() {
  return this.lastName;
}

Person.prototype.getAge = function() {
  return this.age;
}

//Define the student constructor
function Student(firstName, lastName, age, subject) {
    //Assign the instance variables including the new subject variable
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
    this.subject = subject;

    //Add a new function to get the subject from Student
    this.getSubject = function() {
         return this.subject;
    }
}
Student.prototype = Object.create(Person.prototype);
Student.prototype.constructor = Student;

//Testing the inheritance
var joe = new Student("Joe", "Shmo", 33, "Computer Science");
console.log("First Name: " + joe.getFirstName()); //The getFirstName() function is defined in the superclass
console.log("Subject: " + joe.getSubject()); //The getSubject() function is defined in the subclass