-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassintro.js
More file actions
32 lines (32 loc) · 1.94 KB
/
classintro.js
File metadata and controls
32 lines (32 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Product{
// name;
// price;
// rating;
//#rating;//now we have declared the rating data member as private so it can't be accesed outside the class,using#
constructor(n,p,r){
this.name=n;//this keyword always point towards the plain object which has been created
this.price=p;
this.rating=r;
console.log("Calling the constructor")//whenever object is being created the constructor function is called
//if we have not defined the constructor then default constructor is called otherwise the construtor which we have created wil be called.
//return 10; if you're returning primitive then it will be not returned
return{x:0,y:20}//if you're returning non-primitive then it will be returned.
//the main work of constructor is to return the object , if we have not created any custom object then it will return the object to which this keyword is pointing.
//and if we have created an custom object it will return the custom object and will not return the object to which this keyword is pointing.
}
display(){//we have the access of this keyword in member functions
console.log("Displaying the current product",this.name,this.price,this.rating)
}
static custom(){
console.log("static function is called")//custom is a static function
}
}
const p=new Product("Iphone",70000,5)//new creates an empty plain object.
//in the above piece of code we are calling the constructor method.
const o=new Product("Samsung",90000,5);
console.log(p)
//o.display();//this keyword refers to calling context and point towards the object if we obejct o is calling the member fucntion then it will show the key-values of the object o on calling the display function
//p.display();
//and p calls the member function after object o,so it will show the key-value of object p after object o
Product.custom();
//static funtion are always called using the class name , and not by the object name.