LoginSignup
8
8

More than 5 years have passed since last update.

BabelのLearn ES6を学んでいく(3

Posted at

https://babeljs.io/docs/learn-es6/#map-set-weak-map-weak-set
前回に引き続きBabelのLearn ES6で新しいJavaScriptを学んでいきます。なにか間違いありましたらご指摘いただけると助かります。

Map + Set + WeakMap + WeakSet

  • MapやSetがサポートされました
  • Weak系はsizeプロパティを持ちません
  • またWeak系は弱い参照により、登録した要素がどこからも参照されていない場合、要素を保持しません
  • 例:http://www.es6fiddle.net/i8ar4ox7/

Symbols

  • Symbolは新しいプリミティブ型です
  • Symbol(<key>)で定義します。
  • keyが同一であってもSymbolは別扱いになります
  • 例:http://www.es6fiddle.net/i8arqd64/

Subclassable Built-ins

Math + Number + String + Object APIs

  • APIが多数追加されました
  • 一部動作しないものがありました
console.log(Number.EPSILON);
console.log(Number.isInteger(Infinity)) // false
console.log(Number.isNaN("NaN")) // false

console.log(Math.acosh(3)) // 1.762747174039086
console.log(Math.hypot(3, 4)) // 5
console.log(Math.imul(Math.pow(2, 32) - 1, Math.pow(2, 32) - 2)) // 2

console.log("abcde".includes("cd") )// true
console.log("abc".repeat(3) )// "abcabcabc"
console.log(Array.of(1, 2, 3) )// Similar to new Array(...), but without special one-arg behavior
//[0, 0, 0].fill(7, 1) // [0,7,7]
//[1,2,3].findIndex(x => x == 2) // 1
//["a", "b", "c"].entries() // iterator [0, "a"], [1,"b"], [2,"c"]
//["a", "b", "c"].keys() // iterator 0, 1, 2
//["a", "b", "c"].values() // iterator "a", "b", "c"

class Point{
    constructor(x,y){
    }
}

Object.assign(Point, { origin: new Point(0,0) })
console.log(Point)
実行結果.
C:\workspace\babel\src>babel-node script.js
2.220446049250313e-16
false
false
1.762747174039086
5
2
true
abcabcabc
[ 1, 2, 3 ]
{ [Function: Point] origin: {} }

C:\workspace\babel\src>

Binary and Octal Literals

Promises

Reflect API

  • メタレベルの動的実行のためにリフレクトAPIが導入されました
script.js
var O = {a: 1};
Object.defineProperty(O, 'b', {value: 2});
O[Symbol('c')] = 3;

console.log(Reflect.ownKeys(O)); // ['a', 'b', Symbol(c)]

function C(a, b){
  this.c = a + b;
}
var instance = Reflect.construct(C, [20, 22]);
console.log(instance)
実行結果.
C:\workspace\babel\src>babel-node script.js
[ 'a', 'b', Symbol(c) ]
{ c: 42 }

C:\workspace\babel\src>

Tail Calls

末尾での再帰呼び出しに限り、スタックオーバーフローが起きないことを保証します。

script.js
function factorial(n, acc = 1) {
    "use strict";
    if (n <= 1) return acc;
    return factorial(n - 1, n * acc);
}

// Stack overflow in most implementations today,
// but safe on arbitrary inputs in eS6
factorial(100000)
実行結果.
C:\workspace\babel\src>babel-node script.js

C:\workspace\babel\src>
ダメな例.
function factorial(n, acc = 1) {
    "use strict";
    if (n > 1){
        acc = factorial(n - 1, n * acc);
    }
    return acc;
}

// Stack overflow in most implementations today,
// but safe on arbitrary inputs in eS6
factorial(100000)
実行結果.
C:\workspace\babel\src\script.js:3
function factorial(n) {
                  ^
RangeError: Maximum call stack size exceeded
    at factorial (C:/workspace/babel/src/script.js:1:19)
    at factorial (C:/workspace/babel/src/script.js:4:12)
    at factorial (C:/workspace/babel/src/script.js:4:12)
    at factorial (C:/workspace/babel/src/script.js:4:12)
    at factorial (C:/workspace/babel/src/script.js:4:12)
    at factorial (C:/workspace/babel/src/script.js:4:12)
    at factorial (C:/workspace/babel/src/script.js:4:12)
    at factorial (C:/workspace/babel/src/script.js:4:12)
    at factorial (C:/workspace/babel/src/script.js:4:12)
    at factorial (C:/workspace/babel/src/script.js:4:12)

C:\workspace\babel\src>

以上で終わりになります。

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8