-
Notifications
You must be signed in to change notification settings - Fork 5
/
classes-es6.js
144 lines (122 loc) · 3.13 KB
/
classes-es6.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"use strict";
/**
* JavaScript classes provide a much simpler and clearer syntax to create objects and deal with inheritance.
* The bodies of class declarations and class expressions are executed in strict mode.
*
* To declare a class, you use the 'class' keyword
* class ClassName{ }
*
*References:
* https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes
* http://stackoverflow.com/questions/36099721/javascript-what-is-the-difference-between-function-and-class
*/
// Create a Class named
class MyClass{
}
/*
* Constructor
*
* The constructor method is a special method for creating and initializing an object
* There can only be one special method with the name "constructor" in a class.
*/
class Polygon {
constructor(width, height) { // constructor method
this._width = width;
this._height = height;
}
}
/**
*
* Above example can be achieved by the following function
*/
function PolygonFunc(width, height) {
this._width = width;
this._height = height;
}
/**
*
* What is the difference between Function and Class
*
* JavaScript classes are syntactical sugar over JavaScript's existing prototype-based inheritance
* Classes are in fact "special functions"
*
* In terms of syntax and uses Class is different from Functions
*
*
* DEFINE CLASS BODY
*********************************************************
* Constructor
* -----------
* A special function named 'constructor'
*
* Member Methods
* --------------
* property( parameters… ) {},
*
*
* ES5 Getter Setters
* ------------------
* get property() {},
* set property(value) {}
*
*
* Static methods
* --------------
* Define static methods using 'static' keyword
* Static methods are called without creating the instance of the class
* Those methods are often used to create utility functions for an application.
*/
class Car {
// constructor
constructor(owner) {
this._owner = owner;
this._distance = 0;
this._destination = "";
}
// member method
move(){
this._distance += 1;
}
// getter setters
get destination(){
return this._destination;
}
set destination(location){
this._destination = location;
}
get owner(){
return this._owner;
}
get distance(){
return this._distance;
}
// Static methods
static status(){
return "ACTIVE";
}
}
var car = new Car("John");
console.log(car.owner); // John
console.log(car.destination); // empty string
car.destination = "Town Hall"; // setting the destination
console.log(car.destination); // TownHall
console.log(car.distance); // 0
car.move(); // calling member method
console.log(car.distance); // 1
// use of static method
console.log(car.status()) // ERROR , static functions are not a member of the car object
console.log(Car.status()) // ACTIVE, call static function directly ClassName.yourStaticFunction();
/**
* Hoisting
*
* function declarations are hoisted and class declarations are not.
*/
var p = new Box(); // ReferenceError, can't be used before declaration
class Box {}
/*
* Only methods to be declared inside the class body.
*/
class notWorking {
var privateVar; // ERROR
return 1; // ERROR
};