Examples of Class and Object

Examples of Class and Object

We normally don’t use the keyword function while creating function in class but if want to use then ignore the class keyword use as such.

Example 3 using constructor with argument

<script>

class Demo

{         

constructor(w,h)

            {

                        this.width=w;

                        this.height=h;

            }

            area()

            {

                        console.log(this.width* this.height);

            }

}

let object = new Demo(7,5);

object.area();

</script>

Example 4 using function with argument

<script>

class Demo

{         

            area(w,h)

            {

                        this.width=w;

                        this.height=h;

                        console.log(this.width* this.height);

            }

}

let object = new Demo();

object.area(2,5);

</script>

Example 5 Call object without class

 <script>

function User(name) {

  this.name = name;

  console.log(this.name);

}

let user = new User(“shairy”);

console.log(user); // way to call the object

</script>

2 thoughts on “Examples of Class and Object

Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

*

*