Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Extending Class

I have a base class:

function Monster() {   this.health = 100; }  Monster.prototype.growl = function() {   console.log("Grr!"); } 

That I want to extend and create another class with:

function Monkey extends Monster() {   this.bananaCount = 5; }  Monkey.prototype.eatBanana {   this.bananaCount--;   this.health++; //Accessing variable from parent class monster   this.growl();  //Accessing function from parent class monster } 

I've done quite a bit of research and there appears to be many convoluted solutions for doing this in JavaScript. What would be the simplest and most reliable way of accomplishing this in JS?

like image 379
Lucas Penney Avatar asked Mar 04 '13 00:03

Lucas Penney


People also ask

Can you extend a class in JavaScript?

The extends keyword is used to create a child class of another class (parent). The child class inherits all the methods from another class. Inheritance is useful for code reusability: reuse properties and methods of an existing class when you create a new class.

How do you extend an object in JavaScript?

The extends keyword can be used to extend the objects as well as classes in JavaScript. It is usually used to create a class which is child of another class. Syntax: class childclass extends parentclass {...}

What is class extend?

Definition and Usage The extends keyword extends a class (indicates that a class is inherited from another class). In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories: subclass (child) - the class that inherits from another class.

How do I extend a class in TypeScript?

Just like object-oriented languages such as Java and C#, TypeScript classes can be extended to create new classes with inheritance, using the keyword extends . In the above example, the Employee class extends the Person class using extends keyword.


1 Answers

Updated below for ES6

March 2013 and ES5

This MDN document describes extending classes well:

https://developer.mozilla.org/en-US/docs/JavaScript/Introduction_to_Object-Oriented_JavaScript

In particular, here is now they handle it:

// define the Person Class function Person() {}  Person.prototype.walk = function(){   alert ('I am walking!'); }; Person.prototype.sayHello = function(){   alert ('hello'); };  // define the Student class function Student() {   // Call the parent constructor   Person.call(this); }  // inherit Person Student.prototype = Object.create(Person.prototype);  // correct the constructor pointer because it points to Person Student.prototype.constructor = Student;  // replace the sayHello method Student.prototype.sayHello = function(){   alert('hi, I am a student'); }  // add sayGoodBye method Student.prototype.sayGoodBye = function(){   alert('goodBye'); }  var student1 = new Student(); student1.sayHello(); student1.walk(); student1.sayGoodBye();  // check inheritance alert(student1 instanceof Person); // true  alert(student1 instanceof Student); // true 

Note that Object.create() is unsupported in some older browsers, including IE8:

Object.create browser support

If you are in the position of needing to support these, the linked MDN document suggests using a polyfill, or the following approximation:

function createObject(proto) {     function ctor() { }     ctor.prototype = proto;     return new ctor(); } 

Using this like Student.prototype = createObject(Person.prototype) is preferable to using new Person() in that it avoids calling the parent's constructor function when inheriting the prototype, and only calls the parent constructor when the inheritor's constructor is being called.

May 2017 and ES6

Thankfully, the JavaScript designers have heard our pleas for help and have adopted a more suitable way of approaching this issue.

MDN has another great example on ES6 class inheritance, but I'll show the exact same set of classes as above reproduced in ES6:

class Person {     sayHello() {         alert('hello');     }      walk() {         alert('I am walking!');     } }  class Student extends Person {     sayGoodBye() {         alert('goodBye');     }      sayHello() {         alert('hi, I am a student');     } }  var student1 = new Student(); student1.sayHello(); student1.walk(); student1.sayGoodBye();  // check inheritance alert(student1 instanceof Person); // true  alert(student1 instanceof Student); // true 

Clean and understandable, just like we all want. Keep in mind, that while ES6 is pretty common, it's not supported everywhere:

ES6 browser support

like image 177
Oliver Spryn Avatar answered Sep 20 '22 13:09

Oliver Spryn