Skip to content

Commit 84aca44

Browse files
committed
Add variable scoping feature
1 parent 0c31f8a commit 84aca44

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

README.md

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,43 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
103103

104104
1. ### Variable Scoping
105105

106-
The variable scoping determines the visibility or accessibility of a variable within the certain part of the program or region. In ES6, both `const` and `let` keywords allow developers to declare variables in the block scope.
107-
The `let` statement declares a block-scoped local variable
106+
The variable scoping determines the visibility or accessibility of a variable within the certain part of the program or region.
107+
108+
In ES6, both `const` and `let` keywords allow developers to declare variables in the block scope.
109+
110+
The `let` statement declares a block-scoped local variable which can be reassigned. i.e, `let` declaration creates a mutable variable.
111+
112+
```js
113+
let a = 1;
114+
115+
if (a === 1) {
116+
let a = 2;
117+
118+
console.log(a); //2
119+
}
120+
121+
console.log(a); //1
122+
```
123+
124+
**const** variables are similar to **let** variables but they can't be changed through reassignment. i.e, The const declaration creates a read-only reference to a value.
125+
126+
```js
127+
const a = 1;
128+
129+
if (a === 1) {
130+
const b = 2; // You cannot re-assign the value similar to let variable
131+
132+
console.log(b); //2
133+
}
134+
135+
console.log(a); //1
136+
```
137+
108138

109139
2. ### Arrow functions
110140

111141
The arrow functions provides a more concise syntax for writing function expressions by opting out the function and return keywords using fat arrow(=>) notation. Let's see how this arrow function looks like,
142+
112143
```js
113144
// Function Expression
114145
var multiplyFunc = function(a, b) {
@@ -195,6 +226,31 @@ const square = class Square {
195226
}
196227
```
197228

229+
You can use **extend** keyword to use inheritance. This enables the subclass to get all features of a parent class.
230+
231+
```js
232+
class Vehicle {
233+
constructor(name) {
234+
this.name = name;
235+
}
236+
237+
start() {
238+
console.log(`${this.name} vehicle started`);
239+
}
240+
}
241+
242+
class Car extends Vehicle {
243+
start() {
244+
console.log(`${this.name} car started`);
245+
}
246+
}
247+
248+
const car = new Car('BMW');
249+
console.log(car.start()); // BMW car started
250+
```
251+
252+
**Note:** Even though ES6 classes looks similar to classes in other object oriented languages, such as Java, PHP, etc but they do not work exactly the same way.
253+
198254
### Enhanced object literals
199255
Object literals are extended to support setting the prototype at construction, shorthand for foo: foo assignments, defining methods, making super calls, and computing property names with expressions.
200256

0 commit comments

Comments
 (0)