Repository navigation
Expand file tree
/
Copy pathexport.js
More file actions
56 lines (46 loc) · 1.65 KB
/
Copy pathexport.js
File metadata and controls
56 lines (46 loc) · 1.65 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* ==== export.js ====
* This file is used to represent the use of export feature in ES6
* It allows us to export data which we can import in another application.
* You can also export classes and enums.
*/
export const data = [1,2,3];
//=======Classes
export class Animal{
//constructor(){} or constructor([parameter1], [parameter2],...){}
constructor(type, legs){ //Constructor of the class
//No need to define the member variables beforehand.
this.type = type;
this.legs = legs;
}
//functionName(){} or functionName([parameter1], [parameter2],...){}
makeNoise(sound = 'Loud Noise'){ //A member function of the class.
console.log(sound);
}
//Note that we don't use the "function" keyword to specify that this is a class.
//"static" keyword.
//Can be accessed without creating an instance of the class.
//Static method.
static makeANoise(){
console.log('Meow');
}
//"get" method.
//It is a equivalent to getter method in java.
get metaData(){
return `Type: ${this.type}, Legs: ${this.legs}`;
}
}
///Inheritance
// class <childClassName> extends <parentClassName>{}
export class Cat extends Animal{
// constructor(type, legs, hasTail){
// //"super" keyword is used to pass in the inherited member variables to the parent class for initialization.
// //"super" can also be used to call function from the parent class.
// super(type, legs);
// this.hasTail = hasTail;
// }
//Overriding method from the parent class, a static method in this case.
makeNoise(sound = 'meow'){
console.log(sound);
}
}