0% found this document useful (0 votes)
6 views8 pages

JS11 Class Notes

The document explains the concepts of classes and objects in JavaScript, highlighting the role of prototypes and inheritance. It describes how to create classes using constructors and the use of the 'super' keyword for accessing parent class properties and methods. Additionally, it includes examples and a practice exercise related to creating a User and Admin class, as well as error handling using try-catch blocks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

JS11 Class Notes

The document explains the concepts of classes and objects in JavaScript, highlighting the role of prototypes and inheritance. It describes how to create classes using constructors and the use of the 'super' keyword for accessing parent class properties and methods. Additionally, it includes examples and a practice exercise related to creating a User and Admin class, as well as error handling using try-catch blocks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Classes & Objects

Prototypes in JS
A javaScript object is an entity having state and behavior (properties and method).

JS objects have a special property called prototype.

We can set prototype using _ _ proto _ _

*If object & prototype have same method,


object’s method will be used.
Classes in JS
Class is a program-code template for creating objects.

Those objects will have some state (variables) & some behaviour (functions) inside it.

class MyClass {

constructor( ) { ... }

myMethod( ) { ... }

let myObj = new MyClass( ) ;


Classes in JS
Constructor( ) method is : class MyClass {

automatically invoked by new constructor( ) { ... }

initializes object myMethod( ) { ... }

}
Inheritance in JS
inheritance is passing down properties & methods from parent class to child class.

class Parent {

class Child extends Parent {

*If Child & Parent have same method, child’s


method will be used. [Method Overriding]
super Keyword
The super keyword is used to call the constructor of its parent class to access the parent's
properties and methods.

super( args ) // calls Parent‘s constructor

super.parentMethod( args )
Let‘s Practice
Qs. You are creating a website for your college. Create a class User with 2 properties, name &
email. It also has a method called viewData( ) that allows user to view website data.

Qs. Create a new class called Admin which inherits from User. Add a new method called
editData to Admin that allows it to edit website data.
Error Handling
try-catch

try {

... normal code

} catch ( err ) { //err is error object

... handling error

You might also like