Facebook Pixel
Constructor Functions vs ES6 Classes in JavaScript

How do you inherit with constructor functions in JavaScript?

The answer

Student.prototype = Object.create(Person.prototype); Student.prototype.constructor = Student;. In the constructor: Person.call(this, name). This sets up the prototype chain manually. ES6 extends does this automatically.

More FAQs in Constructor Functions vs ES6 Classes in JavaScript

Classes have cleaner syntax, use extends/super for inheritance, throw without new, are not hoisted, and are always strict. Constructor functions require manual prototype chain setup and do not enforce new. Classes are syntactic sugar over the same prototype system.

Yes. class Person { greet() {} } puts greet on Person.prototype. extends sets up the prototype chain (Student.prototype.__proto__ = Person.prototype). Under the hood, it is the same prototypal inheritance.

Yes. Calling a class constructor without new throws TypeError: Class constructor Person cannot be invoked without 'new'. This prevents the forgotten-new bug that constructor functions have.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Your learning library

Small steps. Big possibilities.

Find your next course. Build something great.

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.