Skip to content

Commit aa02b79

Browse files
committed
Add es2017 code samples
1 parent 6112bf0 commit aa02b79

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,10 +1331,10 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
13311331
```
13321332
But if the function parameter definition or function call only contains a comma, a syntax error will be thrown
13331333
```js
1334-
function func(,) { // SyntaxError: missing formal parameter
1334+
function func1(,) { // SyntaxError: missing formal parameter
13351335
console.log('no args');
13361336
};
1337-
func(,); // SyntaxError: expected expression, got ','
1337+
func1(,); // SyntaxError: expected expression, got ','
13381338
```
13391339
13401340
**Note:** Trailing commas are not allowed in Rest Parameters and JSON.

es2017/5.string-padding.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Pad start
2+
const cardNumber = '01234567891234';
3+
const lastFourDigits = cardNumber.slice(-4);
4+
const maskedCardNumber = lastFourDigits.padStart(cardNumber.length, '*');
5+
console.log(maskedCardNumber); // expected output: "**********1234"
6+
7+
// Pad End
8+
const label1 = "Name";
9+
const label2 = "Phone Number";
10+
const value1 = "John"
11+
const value2 = "(222)-333-3456";
12+
13+
console.log((label1 + ': ').padEnd(20, ' ') + value1);
14+
console.log(label2 + ": " + value2); // Name: John
15+
// Phone Number: (222)-333-3456

es2017/6.shared-memory-atomics.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Atomic operations
2+
const sharedMemory = new SharedArrayBuffer(1024);
3+
const sharedArray = new Uint8Array(sharedMemory);
4+
sharedArray[0] = 10;
5+
6+
Atomics.add(sharedArray, 0, 20);
7+
console.log(Atomics.load(sharedArray, 0)); // 30
8+
9+
Atomics.sub(sharedArray, 0, 10);
10+
console.log(Atomics.load(sharedArray, 0)); // 20
11+
12+
Atomics.and(sharedArray, 0, 5);
13+
console.log(Atomics.load(sharedArray, 0)); // 4
14+
15+
Atomics.or(sharedArray, 0, 1);
16+
console.log(Atomics.load(sharedArray, 0)); // 5
17+
18+
Atomics.xor(sharedArray, 0, 1);
19+
console.log(Atomics.load(sharedArray, 0)); // 4
20+
21+
Atomics.store(sharedArray, 0, 10); // 10
22+
23+
Atomics.compareExchange(sharedArray, 0, 5, 10);
24+
console.log(Atomics.load(sharedArray, 0)); // 10
25+
26+
Atomics.exchange(sharedArray, 0, 10);
27+
console.log(Atomics.load(sharedArray, 0)); //10
28+
29+
Atomics.isLockFree(1); // true
30+
31+
// waiting to be notified
32+
const sharedMemory = new SharedArrayBuffer(1024);
33+
const sharedArray = new Int32Array(sharedMemory);
34+
35+
Atomics.wait(sharedArray, 0, 10);
36+
console.log(sharedArray[0]); // 100
37+
38+
Atomics.store(sharedArray, 0, 100);
39+
Atomics.notify(sharedArray, 0, 1);

es2017/7.trailing-commas.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Params include trailing comma
2+
function func(a,b,) { // declaration
3+
console.log(a, b);
4+
}
5+
func(1,2,); // invocation
6+
7+
// function call only contains a comma
8+
function func1(,) { // SyntaxError: missing formal parameter
9+
console.log('no args');
10+
};
11+
func1(,); // SyntaxError: expected expression, got ','

0 commit comments

Comments
 (0)