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

时间:2016-05-26 16:40:15

标签: javascript inheritance abstraction

我试图让它成为一个JavaScript对象中的一些方法可以被子类继承,但我不想让父类被实例化。以下是我编写的一些代码来说明这一点:

/**
* 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非常有用。

我真的希望能够在父类中定义getName函数,这样我就可以拥有子类继承的功能,例如Student。有没有办法做到这一点?我真的很感激任何帮助!

1 个答案:

答案 0 :(得分:1)

您需要在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