Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript extends class [closed]

What is the right/best way to extend a javascript class so Class B inherits everything from the class A (class B extends A)?

like image 360
xpepermint Avatar asked Feb 13 '10 00:02

xpepermint


People also ask

Can you extend classes 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.

What is super () in JS?

The super keyword is used to access properties on an object literal or class's [[Prototype]], or invoke a superclass's constructor. The super. prop and super[expr] expressions are valid in any method definition in both classes and object literals.

Can an object extend a class?

Object does in fact not have an extends clause (and because the Object class is primordial and has special treatment in the spec, there's no extends Object implicitly present). Additionally, you may observe that the value of Object. 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.


2 Answers

Take a look at Simple JavaScript Inheritance and Inheritance Patterns in JavaScript.

The simplest method is probably functional inheritance but there are pros and cons.

like image 166
cletus Avatar answered Oct 06 '22 01:10

cletus


Douglas Crockford has some very good explanations of inheritance in JavaScript:

  1. prototypal inheritance: the 'natural' way to do things in JavaScript
  2. classical inheritance: closer to what you find in most OO languages, but kind of runs against the grain of JavaScript
like image 37
Annabelle Avatar answered Oct 06 '22 00:10

Annabelle