forked from rse/es6-features
-
Notifications
You must be signed in to change notification settings - Fork 0
/
features.txt
1583 lines (1322 loc) · 42.2 KB
/
features.txt
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
##
## es6-features -- ECMAScript 6 Feature Overview & Comparison
## Copyright (c) 2015 Ralf S. Engelschall <[email protected]>
##
## Permission is hereby granted, free of charge, to any person obtaining
## a copy of this software and associated documentation files (the
## "Software"), to deal in the Software without restriction, including
## without limitation the rights to use, copy, modify, merge, publish,
## distribute, sublicense, and/or sell copies of the Software, and to
## permit persons to whom the Software is furnished to do so, subject to
## the following conditions:
##
## The above copyright notice and this permission notice shall be included
## in all copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
## EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
## MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
## IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
## CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
## TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
## SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
##
Constants
=========
Constants
---------
Support for constants (immutable variables).
6| |const PI| = 3.141593;
6| PI > 3.0;
5| // only in ES5 through the help of object properties
5| // and only in global context and not in a block scope
5| Object.|defineProperty|(typeof global === "object" ? global : window, "|PI|", {
5| value: 3.141593,
5| enumerable: true,
5| |writable: false|,
5| |configurable: false|
5| })
5| PI > 3.0;
Scoping
=======
Block-Scoped Variables
----------------------
Block-scoped variables (and constants) without hoisting.
6| for (|let i| = 0; i < a.length; i++) {
6| |let x| = a[i];
6| ...
6| }
6| for (|let i| = 0; i < b.length; i++) {
6| |let y| = b[i];
6| ...
6| }
6|
6| let callbacks = [];
6| for (|let| i = 0; i <= 2; i++) {
6| callbacks[i] = function () { return i * 2; };
6| }
6| callbacks[0]() === 0;
6| callbacks[1]() === 2;
6| callbacks[2]() === 4;
5| |var i, x, y|;
5| for (i = 0; i < a.length; i++) {
5| x = a[i];
5| ...
5| }
5| for (i = 0; i < b.length; i++)
5| y = b[i];
5| ...
5| }
5|
5| var callbacks = [];
5| for (|var| i = 0; i <= 2; i++) {
5| |(function (i) {|
5| callbacks[i] = function() { return i * 2; };
5| |})(i);|
5| }
5| callbacks[0]() === 0;
5| callbacks[1]() === 2;
5| callbacks[2]() === 4;
Block-Scoped Functions
----------------------
Block-scoped function definitions.
6| {
6| function foo () { return 1; }
6| foo() === 1;
6| {
6| function |foo| () { return |2|; }
6| |foo|() === |2|;
6| }
6| foo() === 1;
6| }
5| // only in ES5 with the help of block-scope emulating
5| // function scopes and function expressions
5| (function () {
5| var foo = function () { return 1; }
5| foo() === 1;
5| |(function () {|
5| var |foo| = function () { return |2|; }
5| |foo|() === |2|;
5| |})();|
5| foo() === 1;
5| })();
Arrow Functions
===============
Expression Bodies
-----------------
More expressive closure syntax.
6| odds = evens.map(|v =>| v + 1);
6| pairs = evens.map(|v => (|{ even: v, odd: v + 1 }|)|);
6| nums = evens.map(|(v, i) =>| v + i);
5| odds = evens.map(|function (v) { return| v + 1; |}|);
5| pairs = evens.map(|function (v) { return| { even: v, odd: v + 1 }; |}|);
5| nums = evens.map(|function (v, i) { return| v + i; |}|);
Statement Bodies
----------------
More expressive closure syntax.
6| nums.forEach(|v => {|
6| if (v % 5 === 0)
6| fives.push(v);
6| |}|)
5| nums.forEach(|function (v) {|
5| if (v % 5 === 0)
5| fives.push(v);
5| |}|);
Lexical `this`
--------------
More intuitive handling of current object context.
6| |this|.nums.forEach(|(v) =>| {
6| if (v % 5 === 0)
6| |this|.fives.push(v);
6| });
5| // variant 1
5| var |self| = |this|;
5| |this|.nums.forEach(|function (v)| {
5| if (v % 5 === 0)
5| |self|.fives.push(v);
5| });
5|
5| // variant 2 (since ECMAScript 5.1 only)
5| |this|.nums.forEach(|function (v)| {
5| if (v % 5 === 0)
5| |this|.fives.push(v);
5| }|.bind(this)|);
Extended Parameter Handling
===========================
Default Parameter Values
------------------------
Simple and intuitive default values for function parameters.
6| function f (x, y |= 7|, z |= 42|) {
6| return x + y + z;
6| }
6| f(1) === 50;
5| function f (x, y, z) {
5| |if (y === undefined)|
5| |y = 7|;
5| |if (z === undefined)|
5| |z = 42|;
5| return x + y + z;
5| };
5| f(1) === 50;
Rest Parameter
--------------
Aggregation of remaining arguments into single parameter of variadic functions.
6| function f (x, y, |...a|) {
6| return (x + y) * a.length;
6| }
6| f(1, 2, |"hello", true, 7|) === 9;
5| function f (x, y) {
5| |var a = Array.prototype.slice.call(arguments, 2)|;
5| return (x + y) * a.length;
5| };
5| f(1, 2, |"hello", true, 7|) === 9;
Spread Operator
---------------
Spreading of elements of an interable collection (like an array or even
a string) into both literal elements and individual function parameters.
6| var params = [ "hello", true, 7 ];
6| var other = [ 1, 2, |...params| ]; // [ 1, 2, "hello", true, 7 ]
6| f(1, 2, |...params|) === 9;
6|
6| var str = "foo";
6| var chars = [ ... str ]; // [ "f", "o", "o" ]
5| var params = [ "hello", true, 7 ];
5| var other = [ 1, 2 ].|concat(params)|; // [ 1, 2, "hello", true, 7 ]
5| f|.apply|(|undefined, [| 1, 2 |].concat(params)|) === 9;
5|
5| var str = "foo";
5| var chars = str.split(""); // [ "f", "o", "o" ]
Template Strings
================
String Interpolation
--------------------
Intuitive expression interpolation for single-line and multi-line strings.
6| var customer = { name: "Foo" };
6| var card = { amount: 7, product: "Bar", unitprice: 42 };
6| message = `Hello |${customer.name}|,
6| want to buy |${card.amount} ${card.product}| for
6| a total of |${card.amount * card.unitprice}| bucks?`;
5| var customer = { name: "Foo" };
5| var card = { amount: 7, product: "Bar", unitprice: 42 };
5| message = "Hello "| + customer.name + |",\n" +
5| "want to buy "| + card.amount + |" "| + card.product + |" for\n" +
5| "a total of "| + (card.amount * card.unitprice) + |" bucks?";
Custom Interpolation
--------------------
Flexible expression interpolation for arbitrary methods.
6| get`http://example.com/foo?bar=|${|bar + baz|}|&quux=|${|quux|}|`;
5| get|([ |"http://example.com/foo?bar="|,| "&quux="|,| ""| ],|bar + baz|,| quux|)|;
Raw String Access
-----------------
Access the raw template string content (backslashes are not interpreted).
6| function quux (strings, ...values) {
6| strings[0] === "|foo\n|";
6| strings[1] === "bar";
6| strings|.raw|[0] === "|foo\\n|";
6| strings.raw[1] === "bar";
6| values[0] === 42;
6| }
6| quux `foo|\n|${ 42 }bar`
6|
6| String.raw `foo|\n|${ 42 }bar` === "foo|\\n|42bar";
5| // no equivalent in ES5
Extended Literals
=================
Binary & Octal Literal
----------------------
Direct support for safe binary and octal literals.
6| |0b|111110111 === 503;
6| |0o|767 === 503;
5| |parseInt(|"111110111"|, 2)| === 503;
5| |parseInt(|"767"|, 8)| === 503;
5| |0|767 === 503; // only in non-strict, backward compatibility mode
Unicode String & RegExp Literal
-------------------------------
Extended support using Unicode within strings and regular expressions.
6| "𠮷".length === 2;
6| "𠮷".match(/./|u|)[0].length === 2;
6| "|\u{20BB7}|" === "𠮷" === "\uD842\uDFB7";
6| "𠮷".|codePointAt|(0) == 0x20BB7;
6| for (let codepoint |of| "𠮷") console.log(codepoint);
5| "𠮷".length === 2;
5| "𠮷".match(/(?:[\0-\t\x0B\f\x0E-\u2027\u202A-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])/)[0].length === 2;
5| "𠮷" === "\uD842\uDFB7";
5| // no equivalent in ES5
5| // no equivalent in ES5
Enhanced Regular Expression
===========================
Regular Expression Sticky Matching
----------------------------------
Keep the matching position sticky between matches and
this way support efficient parsing of arbitrary long input strings,
even with an arbitrary number of distinct regular expressions.
6| let parser = (input, match) => {
6| for (let pos = 0, lastPos = input.length; |pos < lastPos|; ) {
6| for (let i = 0; i < match.length; i++) {
6| |match[i].pattern.lastIndex = pos|;
6| let found;
6| if ((found = match[i].pattern.exec(input)) !== null) {
6| match[i].action(found);
6| |pos = match[i].pattern.lastIndex|;
6| break;
6| }
6| }
6| }
6| }
6|
6| let report = (match) => {
6| console.log(JSON.stringify(match));
6| };
6| parser("Foo 1 Bar 7 Baz 42", [
6| { pattern: /^Foo\s+(\d+)/|y|, action: (match) => report(match) },
6| { pattern: /^Bar\s+(\d+)/|y|, action: (match) => report(match) },
6| { pattern: /^Baz\s+(\d+)/|y|, action: (match) => report(match) },
6| { pattern: /^\s*/|y|, action: (match) => {} }
6| ]);
5| var parser = function (input, match) {
5| for (var i, found, |inputTmp = input|; |inputTmp !== ""|; ) {
5| for (i = 0; i < match.length; i++) {
5| if ((found = match[i].pattern.exec(inputTmp)) !== null) {
5| match[i].action(found);
5| |inputTmp = inputTmp.substr(found[0].length)|;
5| break;
5| }
5| }
5| }
5| }
5|
5| var report = function (match) {
5| console.log(JSON.stringify(match));
5| };
5| parser("Foo 1 Bar 7 Baz 42", [
5| { pattern: /^Foo\s+(\d+)/, action: function (match) { report(match); } },
5| { pattern: /^Bar\s+(\d+)/, action: function (match) { report(match); } },
5| { pattern: /^Baz\s+(\d+)/, action: function (match) { report(match); } },
5| { pattern: /^\s*/, action: function (match) {} }
5| ]);
Enhanced Object Properties
==========================
Property Shorthand
------------------
Shorter syntax for common object property definition idiom.
6| obj = { |x|, |y| };
5| obj = { |x: x|, |y: y| };
Computed Property Names
-----------------------
Support for computed names in object property definitions.
6| obj = {
6| foo: "bar",
6| |[| "prop_" + foo() |]:| 42
6| };
5| obj = {
5| foo: "bar"
5| };
5| |obj[| "prop_" + foo() |] =| 42;
Method Properties
-----------------
Support for method notation in object property definitions,
for both regular functions and generator functions.
6| obj = {
6| |foo (a, b)| {
6| ...
6| },
6| |bar (x, y)| {
6| ...
6| },
6| |*quux (x, y)| {
6| ...
6| }
6| };
5| obj = {
5| foo|: function| (a, b) {
5| ...
5| },
5| bar|: function| (x, y) {
5| ...
5| },
5| // quux: no equivalent in ES5
5| };
Destructuring Assignment
========================
Array Matching
--------------
Intuitive and flexible destructuring of Arrays into individual variables during assignment.
6| var list = [ 1, 2, 3 ];
6| var |[ a, , b ] = list|;
6| [ |b, a| ] = [ |a, b| ];
5| var list = [ 1, 2, 3 ];
5| var |a = list[0]|, |b = list[2]|;
5| var |tmp = a|; |a = b|; |b = tmp|;
Object Matching, Shorthand Notation
-----------------------------------
Intuitive and flexible destructuring of Objects into individual variables during assignment.
6| var |{ op, lhs, rhs }| = getASTNode();
5| var |tmp| = getASTNode();
5| var |op = tmp.op|;
5| var |lhs = tmp.lhs|;
5| var |rhs = tmp.rhs|;
Object Matching, Deep Matching
------------------------------
Intuitive and flexible destructuring of Objects into individual variables during assignment.
6| var { op: a, |lhs: { op: b }|, rhs: c } = getASTNode();
5| var tmp = getASTNode();
5| var a = tmp.op;
5| var |b = tmp.lhs.op|;
5| var c = tmp.rhs;
Parameter Context Matching
--------------------------
Intuitive and flexible destructuring of Arrays and Objects into individual parameters during function calls.
6| function f (|[ name, val ]|) {
6| console.log(name, val);
6| }
6| function g (|{ name: n, val: v }|) {
6| console.log(n, v);
6| }
6| function h (|{ name, val }|) {
6| console.log(name, val);
6| }
6| f([ "bar", 42 ]);
6| g({ name: "foo", val: 7 });
6| h({ name: "bar", val: 42 });
5| function f (|arg|) {
5| |var name = arg[0]|;
5| |var val = arg[1]|;
5| console.log(name, val);
5| };
5| function g (|arg|) {
5| |var n = arg.name|;
5| |var v = arg.val|;
5| console.log(n, v);
5| };
5| function h (|arg|) {
5| |var name = arg.name|;
5| |var val = arg.val|;
5| console.log(name, val);
5| };
5| f([ "bar", 42 ]);
5| g({ name: "foo", val: 7 });
5| h({ name: "bar", val: 42 });
Fail-Soft Destructuring
-----------------------
Fail-soft destructuring, optionally with defaults.
6| var list = [ 7, 42 ];
6| var [ a = 1, b = 2, |c = 3|, |d| ] = list;
6| a === 7;
6| b === 42;
6| c === |3|;
6| d === |undefined|;
5| var list = [ 7, 42 ];
5| var a = typeof list[0] !== "undefined" ? list[0] : 1;
5| var b = typeof list[1] !== "undefined" ? list[1] : 2;
5| var c = typeof list[2] !== "undefined" ? list[2] : |3|;
5| var d = typeof list[3] !== "undefined" ? list[3] : |undefined|;
5| a === 7;
5| b === 42;
5| c === |3|;
5| d === |undefined|;
Modules
=======
Symbol Export/Import
--------------------
Support for exporting/importing symbols from/to modules without global namespace pollution.
6| // lib/math.js
6| |export| function |sum| (x, y) { return x + y };
6| |export| var |pi| = 3.141593;
6|
6| // someApp.js
6| |import * as math| from "lib/math";
6| console.log("2π = " + math.sum(math.pi, math.pi));
6|
6| // otherApp.js
6| |import { sum, pi }| from "lib/math";
6| console.log("2π = " + sum(pi, pi));
5| // lib/math.js
5| |LibMath = {}|;
5| |LibMath.sum| = function (x, y) { return x + y };
5| |LibMath.pi| = 3.141593;
5|
5| // someApp.js
5| var |math = LibMath|;
5| console.log("2π = " + math.sum(math.pi, math.pi));
5|
5| // otherApp.js
5| var |sum = LibMath.sum|, |pi = LibMath.pi|;
5| console.log("2π = " + sum(pi, pi));
Default & Wildcard
------------------
Marking a symbol as the default exported symbol and mass-mixin of symbols.
6| // lib/mathplusplus.js
6| |export *| from "lib/math";
6| export var e = 2.71828182846;
6| |export default| (x) => Math.exp(x);
6|
6| // someApp.js
6| |import exp, { pi, e }| from "lib/mathplusplus";
6| console.log("e^{π} = " + exp(pi));
5| // lib/mathplusplus.js
5| |LibMathPP = {}|;
5| |for (symbol in LibMath)|
5| |if (LibMath.hasOwnProperty(symbol))|
5| |LibMathPP[symbol] = LibMath[symbol]|;
5| |LibMathPP.e| = 2.71828182846;
5| |LibMathPP.exp| = function (x) { return Math.exp(x) };
5|
5| // someApp.js
5| var |exp = LibMathPP.exp|, |pi = LibMathPP.pi|, |e = libMathPP.e|;
5| console.log("e^{π} = " + exp(pi));
Classes
=======
Class Definition
----------------
More intuitive, OOP-style and boilerplate-free classes.
6| |class| Shape {
6| |constructor| (id, x, y) {
6| this.id = id;
6| this.move(x, y);
6| }
6| |move| (x, y) {
6| this.x = x;
6| this.y = y;
6| }
6| }
5| var Shape = |function| (id, x, y) {
5| this.id = id;
5| this.move(x, y);
5| };
5| Shape.|prototype.move| = |function| (x, y) {
5| this.x = x;
5| this.y = y;
5| };
Class Inheritance
-----------------
More intuitive, OOP-style and boilerplate-free inheritance.
6| |class| Rectangle |extends| Shape {
6| constructor (id, x, y, width, height) {
6| |super|(id, x, y);
6| this.width = width;
6| this.height = height;
6| }
6| }
6| |class| Circle |extends| Shape {
6| constructor (id, x, y, radius) {
6| |super|(id, x, y);
6| this.radius = radius;
6| }
6| }
5| var Rectangle = function (id, x, y, width, height) {
5| |Shape.call|(this, id, x, y);
5| this.width = width;
5| this.height = height;
5| };
5| |Rectangle.prototype = Object.create(Shape.prototype)|;
5| |Rectangle.prototype.constructor = Rectangle|;
5| var Circle = function (id, x, y, radius) {
5| |Shape.call|(this, id, x, y);
5| this.radius = radius;
5| };
5| |Circle.prototype = Object.create(Shape.prototype)|;
5| |Circle.prototype.constructor = Circle|;
Class Inheritance, From Expressions
-----------------------------------
Support for mixin-style inheritance by extending from expressions yielding
function objects. [Notice: the generic `aggregation` function is usually provided
by a library like [this one](https://github.com/rse/aggregation), of course]
6| var |aggregation| = (baseClass, ...mixins) => {
6| let base = class _Combined extends baseClass {
6| constructor (...args) {
6| |super|(...args);
6| mixins.forEach((mixin) => {
6| mixin.prototype.|initializer|.call(this);
6| });
6| }
6| };
6| let copyProps = (target, source) => {
6| Object.getOwnPropertyNames(source)
6| .concat(Object.getOwnPropertySymbols(source))
6| .forEach((prop) => {
6| if (prop.match(/^(?:constructor@3@prototype@3@arguments@3@caller@3@name@3@bind@3@call@3@apply@3@toString@3@length)$/))
6| return
6| Object.defineProperty(target, prop, Object.getOwnPropertyDescriptor(source, prop))
6| })
6| }
6| mixins.forEach((mixin) => {
6| copyProps(base.prototype, mixin.prototype);
6| copyProps(base, mixin);
6| });
6| return base;
6| };
6|
6| class |Colored| {
6| |initializer| () { this._color = "white"; }
6| get color () { return this._color; }
6| set color (v) { this._color = v; }
6| }
6|
6| class |ZCoord| {
6| |initializer| () { this._z = 0; }
6| get z () { return this._z; }
6| set z (v) { this._z = v; }
6| }
6|
6| class |Shape| {
6| |constructor| (x, y) { this._x = x; this._y = y; }
6| get x () { return this._x; }
6| set x (v) { this._x = v; }
6| get y () { return this._y; }
6| set y (v) { this._y = v; }
6| }
6|
6| class Rectangle extends |aggregation(Shape, Colored, ZCoord)| {}
6|
6| var rect = new Rectangle(7, 42);
6| rect.|z| = 1000;
6| rect.|color| = "red";
6| console.log(rect.x, rect.y, rect.z, rect.color);
5| var |aggregation| = function (baseClass, mixins) {
5| var base = function () {
5| |baseClass.apply|(this, arguments);
5| mixins.forEach(function (mixin) {
5| mixin.prototype.|initializer|.call(this);
5| }.bind(this));
5| };
5| base.prototype = Object.create(baseClass.prototype);
5| base.prototype.constructor = base;
5| var copyProps = function (target, source) {
5| Object.getOwnPropertyNames(source).forEach(function (prop) {
5| if (prop.match(/^(?:constructor@3@prototype@3@arguments@3@caller@3@name@3@bind@3@call@3@apply@3@toString@3@length)$/))
5| return
5| Object.defineProperty(target, prop, Object.getOwnPropertyDescriptor(source, prop))
5| })
5| }
5| mixins.forEach(function (mixin) {
5| copyProps(base.prototype, mixin.prototype);
5| copyProps(base, mixin);
5| });
5| return base;
5| };
5|
5| var Colored = function () {};
5| Colored.prototype = {
5| |initializer|: function () { this._color = "white"; },
5| getColor: function () { return this._color; },
5| setColor: function (v) { this._color = v; }
5| };
5|
5| var ZCoord = function () {};
5| ZCoord.prototype = {
5| |initializer|: function () { this._z = 0; },
5| getZ: function () { return this._z; },
5| setZ: function (v) { this._z = v; }
5| };
5|
5| var Shape = function (x, y) {
5| this._x = x; this._y = y;
5| };
5| Shape.prototype = {
5| getX: function () { return this._x; },
5| setX: function (v) { this._x = v; },
5| getY: function () { return this._y; },
5| setY: function (v) { this._y = v; }
5| }
5|
5| var _Combined = |aggregation(Shape, [ Colored, ZCoord ])|;
5| var Rectangle = function (x, y) {
5| _Combined.call(this, x, y);
5| };
5| Rectangle.prototype = Object.create(_Combined.prototype);
5| Rectangle.prototype.constructor = Rectangle;
5|
5| var rect = new Rectangle(7, 42);
5| rect.|setZ|(1000);
5| rect.|setColor|("red");
5| console.log(rect.getX(), rect.getY(), rect.getZ(), rect.getColor());
Base Class Access
-----------------
Intuitive access to base class constructor and methods.
6| class Shape {
6| ...
6| |toString| () {
6| return `Shape(${this.id})`
6| }
6| }
6| class Rectangle extends Shape {
6| constructor (id, x, y, width, height) {
6| |super|(id, x, y);
6| ...
6| }
6| |toString| () {
6| return "Rectangle > " + |super.toString()|;
6| }
6| }
6| class Circle extends Shape {
6| constructor (id, x, y, radius) {
6| |super|(id, x, y);
6| ...
6| }
6| |toString| () {
6| return "Circle > " + |super.toString()|;
6| }
6| }
5| var Shape = |function| (id, x, y) {
5| ...
5| };
5| Shape.|prototype.toString| = function (x, y) {
5| return "Shape(" + this.id + ")"
5| };
5| var Rectangle = function (id, x, y, width, height) {
5| |Shape.call|(this, id, x, y);
5| ...
5| };
5| Rectangle|.prototype.toString| = function () {
5| return "Rectangle > " + |Shape.prototype.toString.call(this)|;
5| };
5| var Circle = function (id, x, y, radius) {
5| |Shape.call|(this, id, x, y);
5| ...
5| };
5| Circle|.prototype.toString| = function () {
5| return "Circle > " + |Shape.prototype.toString.call(this)|;
5| };
Static Members
--------------
Simple support for static class members.
6| class Rectangle extends Shape {
6| ...
6| |static defaultRectangle| () {
6| return new Rectangle("default", 0, 0, 100, 100);
6| }
6| }
6| class Circle extends Shape {
6| ...
6| |static defaultCircle| () {
6| return new Circle("default", 0, 0, 100);
6| }
6| }
6| var defRectangle = |Rectangle.defaultRectangle()|;
6| var defCircle = |Circle.defaultCircle()|;
5| var Rectangle = function (id, x, y, width, height) {
5| ...
5| };
5| |Rectangle.defaultRectangle| = function () {
5| return new Rectangle("default", 0, 0, 100, 100);
5| };
5| var Circle = function (id, x, y, width, height) {
5| ...
5| };
5| |Circle.defaultCircle| = function () {
5| return new Circle("default", 0, 0, 100);
5| };
5| var defRectangle = |Rectangle.defaultRectangle()|;
5| var defCircle = |Circle.defaultCircle()|;
Getter/Setter
-------------
Getter/Setter also directly within classes (and not just within object
initializers, as it is possible since ECMAScript 5.1).
6| class Rectangle {
6| constructor (width, height) {
6| this.width = width;
6| this.height = height;
6| }
6| |set| width (width) { this._width = width; }
6| |get| width () { return this._width; }
6| |set| height (height) { this._height = height; }
6| |get| height () { return this._height; }
6| |get| area () { return this.width * this.height; }
6| };
6| var r = new Rectangle(50, 20);
6| r.area === 1000;
5| var Rectangle = function (width, height) {
5| this.width = width;
5| this.height = height;
5| };
5| Rectangle|.prototype| = {
5| |set| width (width) { this._width = width; }|,|
5| |get| width () { return this._width; }|,|
5| |set| height (height) { this._height = height; }|,|
5| |get| height () { return this._height; }|,|
5| |get| area () { return this.width * this.height; }
5| };
5| var r = new Rectangle(50, 20);
5| r.area === 1000;
Symbol Type
===========
Symbol Type
-----------
Unique and immutable data type to be used as an identifier for object properties.
Symbol can have an optional description, but for debugging purposes only.
6| Symbol("foo") |!==| Symbol("foo");
6| const foo = Symbol();
6| const bar = Symbol();
6| typeof foo === |"symbol"|;
6| typeof bar === |"symbol"|;
6| let obj = {};
6| obj[foo] = "foo";
6| obj[bar] = "bar";
6| JSON.stringify(obj); // {}
6| Object.keys(obj); // []
6| Object.getOwnPropertyNames(obj); // []
6| Object.|getOwnPropertySymbols|(obj); // [ foo, bar ]
5| // no equivalent in ES5
Global Symbols
--------------
Global symbols, indexed through unique keys.
6| Symbol.for("app.foo") |===| Symbol.for("app.foo")
6| const foo = Symbol.for("app.foo");
6| const bar = Symbol.for("app.bar");
6| Symbol.keyFor(foo) === "app.foo";
6| Symbol.keyFor(bar) === "app.bar";
6| typeof foo === |"symbol"|;
6| typeof bar === |"symbol"|;
6| let obj = {};
6| obj[foo] = "foo";
6| obj[bar] = "bar";
6| JSON.stringify(obj); // {}
6| Object.keys(obj); // []
6| Object.getOwnPropertyNames(obj); // []
6| Object.|getOwnPropertySymbols|(obj); // [ foo, bar ]
5| // no equivalent in ES5
Iterators
=========
Iterator & For-Of Operator
--------------------------
Support "iterable" protocol to allow objects to customize their
iteration behaviour. Additionally, support "iterator" protocol to produce
sequence of values (either finite or infinite). Finally, provide
convenient `of` operator to iterate over all values of an iterable object.
6| let fibonacci = {
6| |[Symbol.iterator]()| {
6| let pre = 0, cur = 1;
6| return {
6| |next ()| {
6| [ pre, cur ] = [ cur, pre + cur ];
6| return |{ done: false, value: cur }|;
6| }
6| };
6| }
6| }
6|
6| for (let n |of| fibonacci) {
6| if (n > 1000)
6| break;
6| console.log(n);
6| }
5| var fibonacci = {
5| |next:| ((function () {
5| var pre = 0, cur = 1;
5| return function () {
5| tmp = pre;
5| pre = cur;
5| cur += tmp;
5| return cur;
5| };
5| })();
5| };
5|
5| var n;
5| for (;;) {
5| n = fibonacci|.next()|;
5| if (n > 1000)
5| break;
5| console.log(n);
5| }
Generators
==========
Generator Function, Iterator Protocol
-------------------------------------
Support for generators, a special case of Iterators containing a
generator function, where the control flow can be paused and resumed, in
order to produce sequence of values (either finite or infinite).
6| let fibonacci = {
6| |*[Symbol.iterator]()| {
6| let pre = 0, cur = 1;
6| |for (;;)| {
6| [ pre, cur ] = [ cur, pre + cur ];
6| |yield cur|;
6| }