MOCKSTACKS
EN
Questions And Answers

More Tutorials




Calling the base constructor in Javascript


Take two classes one is for the parent class and another is for the child class for inherited the properties and methods of the parent class so for that we should have to use extends keyword to inherit child class from the parent class. Then inside the child class constructor, we have to call the parent class constructor otherwise the compiler will throughs an error and tell you to mention or call the constructor within the child class constructor method before accessing this.

<script>
	class Geeks {
		constructor(num1) {
			this.num1 = num1;
		}
		fun() {
			console.log("Parent class method call");
		}
	}
	class Myclass extends Geeks {
		constructor(num1, num2) {

			// Calling parent class constructor
			super(num1);
			this.num2 = num2;
		}
		fun() {
			super.fun();
			console.log("child class method call");
		}
	}
	let obj = new Myclass(1, 2);
	obj.fun();
</script>

Output

Parent class method call
child class method call

This cannot be used in a child class constructor until super has been called. In ES6, constructors for subclasses are required to call super, or they must return some object in place of the one that was never initialized. In this example, Geeks class is the parent class of MyClass and when an object is created of type MyClass first it calls the MyClass constructor inside MyClass constructor we have to call the parent class constructor before accessing “this” (own object). So it first called the parent class(Geeks class)constructor before it access the object of its own.

<script>
	class Geeks {
		constructor(num) {
			this.num = num;
			console.log("inside Parent Class constructor");
		}
	}
	class MyClass extends Geeks {
		constructor(num1, num2) {
			super(num1);
			this.num2 = num2;
			console.log("inside Child class Constructor");
		}
	}
	let obj = new MyClass(1, 2);
</script>

Output

inside Parent Class constructor
inside Child class Constructor


Conclusion

In this page (written and validated by ) you learned about . What's Next? If you are interested in completing Javascript tutorial, we encourage you simply to start here: Javascript Tutorial.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.