Skip to content

Commit 26c1f78

Browse files
committed
Add import feature
1 parent 94cfbb3 commit 26c1f78

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

README.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,57 @@ greet(); // Hello World!
147147
### Classes
148148
The classes are introduced as syntactic sugar over existing prototype based inheritance and constructor functions. So this feature doesn't bring new object-oriented inheritance model to JavaScript.
149149

150+
### Modules
151+
Modules are small units of independent, reusable code to be used as the building blocks in a Javascript application.
152+
153+
Prior to ES6, there was no native modules support in JavaScript. There were 3 major module standards used,
154+
155+
1. Asynchronous Module Definition (AMD)
156+
2. RequireJS Modules
157+
3. CommonJS Modules (module.exports and require syntax used in Node.js)
158+
159+
ES6 has provided the built-in support for modules. Everything inside a module is private by default, and runs in strict mode. Public variables, functions and classes are exposed using `export` statement.
160+
161+
**Export Statement:**
162+
There are two types of exports:
163+
164+
1. Named Exports (Zero or more exports per module)
165+
166+
You can export each element or a single export statement to export all the elements at once
167+
168+
```js
169+
const PI = Math.PI;
170+
171+
function add(...args) {
172+
return args.reduce((num, tot) => tot + num);
173+
}
174+
175+
function multiply(...args) {
176+
return args.reduce((num, tot) => tot * num);
177+
}
178+
179+
// private function
180+
function print(msg) {
181+
console.log(msg);
182+
}
183+
184+
export { PI, add, multiply };
185+
```
186+
187+
2. Default Exports (One per module)
188+
189+
If we want to export a single value, you could use a default export
190+
191+
```js
192+
// module "my-module.js"
193+
194+
export default function add(...args) {
195+
return args.reduce((num, tot) => tot + num);
196+
}
197+
```
198+
199+
**Import Statement:**
200+
150201
### Set
151202
Set is a built-in object to store collections of unique values of any type.
152203
```js
@@ -467,6 +518,85 @@ let john = Reflect.construct(
467518
console.log(john instanceof User);
468519
console.log(john.fullName); // John Doe
469520
```
521+
522+
2. **Calling a function using Reflect.apply():**
523+
Prior to ES6, you can invoke a function with a specified `this` value and arguments by using the `Function.prototype.apply()` method.
524+
525+
For example, you can call `max()` static method of Math object,
526+
```js
527+
const max = Function.prototype.apply.call(Math.max, Math, [100, 200, 300]);
528+
console.log(max);
529+
```
530+
531+
In ES6, Reflect.apply() provides the same features as Function.prototype.apply() but in a less verbose syntax.
532+
533+
```js
534+
const max = Reflect.apply(Math.max, Math, [100, 200, 300]);
535+
console.log(max);
536+
```
537+
3. **Defining a property using Reflect.defineProperty():**
538+
The `Reflect.defineProperty()` method is similar to `Object.defineProperty()` but it returns a Boolean value indicating whether or not the property was defined successfully instead of throwing an exception.
539+
540+
The syntax of this method looks like below,
541+
```js
542+
Reflect.defineProperty(target, propertyName, propertyDescriptor)
543+
```
544+
545+
Let's define the age property on user object,
546+
```js
547+
class User {
548+
constructor(firstName, lastName) {
549+
this.firstName = firstName;
550+
this.lastName = lastName;
551+
}
552+
get fullName() {
553+
return `${this.firstName} ${this.lastName}`;
554+
}
555+
};
556+
557+
let john = new User('John', 'Resig');
558+
559+
if (Reflect.defineProperty(john, 'age', {
560+
writable: true,
561+
configurable: true,
562+
enumerable: false,
563+
value: 33,
564+
})) {
565+
console.log(john.age);
566+
} else {
567+
console.log('Cannot define the age property on the user object.');
568+
569+
}
570+
```
571+
572+
4. **Delete property using Reflect.deleteProperty():**
573+
574+
The `Reflect.deleteProperty()` method is used to delete properties like the delete operator but as a function. It returns Boolean value indicating whether or not the property was successfully deleted.
575+
576+
```js
577+
const user = {
578+
name: 'John',
579+
age: 33
580+
};
581+
582+
console.log(Reflect.deleteProperty(user, 'age')); // true
583+
console.log(user.age); // undefined
584+
```
585+
586+
5. **Get property of an object using Reflect.get():**
587+
The `Reflect.get` method is used to get a property on an object like the property accessor syntax but as a function.
588+
589+
```js
590+
const user = {
591+
name: 'John',
592+
age: 33
593+
};
594+
595+
console.log(Reflect.get(user, 'age')); // 33
596+
```
597+
598+
6. **:**
599+
470600
### Proper Tail Calls
471601
**Proper tail call(PTC)** is a technique where the program or code will not create additional stack frames for a recursion when the function call is a tail call.
472602

0 commit comments

Comments
 (0)