WellKnownSymbols (WN03)
1: const alpha = ['a', 'b', 'c'];
2: const numeric = [1, 2, 3];
3: numeric[Symbol.isConcatSpreadable] = false; // numeric won't be flattened during concatenation
4: let alphaNumeric = alpha.concat(numeric);
5: console.log(alphaNumeric); // Output: ['a', 'b', 'c', [1, 2, 3]]. numeric isn't flattened.
6: numeric[Symbol.isConcatSpreadable] = true; //numeric array will be spread when concatinated
7: alphaNumeric = alpha.concat(numeric);
8: console.log(alphaNumeric); // ['a', 'b', 'c', 1, 2, 3];
Symbol.isConcatSpreadable controls if an object is flattened (spread) when used with concat() [ 'a', 'b', 'c', [ 1, 2, 3, [Symbol(Symbol.isConcatSpreadable)]: false ] ] [ 'a', 'b', 'c', 1, 2, 3 ]
WellKnownSymbols context:
ES6 context:
- (2024) Notes about JS Closures. #ES6
- (2024) Notes about Javascript asynchronous programming. #ES6
- (2022) Modern Javascript books #ES6 #Doc
- (2021) JS learning start point #ES6
- (2021) Maximilian Schwarzmüller Javascript lecture #ES6
- (2021) Javascript interview question from Happy Rawat #ES6
- (2021) Javascript tests #ES6
- (2016) New unique features of Javascript (updated). #ES6
Comments (
)

Link to this page:
http://www.vb-net.com/JavascriptES6/WN03.htm
|