understanding es6

chapter 3

Rest Parameters

function pick(object, ...keys) {
let result = Object.create(null);
for (let i = 0, len = keys.length; i < len; i++) {
result[keys[i]] = object[keys[i]];
}
return result;
}

Rest parameters have two restrictions. The first restriction is that there can be only one rest parameter, and the rest parameter must be last.The second restriction is that rest parameters cannot be used in an object literal setter. That means this code would also cause a syntax error:

let object = {
// Syntax error: Can't use rest param in setter
set name(...value) {
// do something
}
};

This restriction exists because object literal setters are restricted to a single argument. Rest parameters are, by definition, an infinite number of
arguments

arrow function

But an arrow function that wants to return an object literal outside a function body must wrap the literal in parentheses. For example:

let getTempItem = id => ({ id: id, name: "Temp" });
// effectively equivalent to:
let getTempItem = function(id) {
return {
id: id,
name: "Temp"
};
};

Chapter 7 sets and maps

Sets

ECMAScript 6 adds a Set type that is an ordered list of values without duplicates

let set = new Set();
set.add(5);
set.add("5");
console.log(set.has(5)); // true
set.delete(5);
console.log(set.has(5)); // false
console.log(set.size); // 1
set.clear();
console.log(set.has("5")); // false
console.log(set.size);

let set = new Set([1, 2, 3, 3, 3, 4, 5])
set.forEach(function(value, key, ownerSet) {
  console.log(key + " " + value);
  console.log(ownerSet === set);
});
array = [...set];
console.log(array); 

ECMAScript 6 also includes weak sets, which only store weak object references and cannot store primitive values

  • sets vs weak sets
    • In a WeakSet instance, the add() method, has() method, and delete()
    method all throw an error when passed a nonobject.
    • Weak sets aren’t iterables and therefore cannot be used in a for-of loop.
    • Weak sets don’t expose any iterators (such as the keys() and values()
    methods), so there is no way to programmatically determine the con-
    tents of a weak set.
    • Weak sets don’t have a forEach() method.
    • Weak sets don’t have a size property.

Maps

The ECMAScript 6 Map type is an ordered list of key-value pairs, where the key and the value can be any type

let map = new Map();
map.set("name", "Nicholas");
map.set("age", 25);
console.log(map.size); // 2
console.log(map.has("name")); // true
console.log(map.get("name")); // "Nicholas"
console.log(map.has("age")); // true
console.log(map.get("age")); // 25
map.delete("name");
console.log(map.has("name")); // false
console.log(map.get("name")); // undefined
console.log(map.size); // 1
map.clear();
console.log(map.has("name")); // false
console.log(map.get("name")); // undefined
console.log(map.has("age")); // false
console.log(map.get("age")); // undefined
console.log(map.size); // 0

let map = new Map([["name", "Nicholas"], ["age", 25]]);

let map = new Map([["name", "Nicholas"], ["age", 25]]);
map.forEach(function(value, key, ownerMap) {
console.log(key + " " + value);
console.log(ownerMap === map);
});

Chapter8 Iterators and Generators

Iterators are objects with a specific interface designed for iteration. All iterator objects have a next() method that returns a result object
A generator is a function that returns an iterator
You can use the yield keyword only inside generators. Using yield anywhere
else is a syntax error, including in functions that are inside generators, such as:

function *createIterator(items) {
items.forEach(function(item) {
// syntax error
yield item + 1;
});
}

note: Creating an arrow function that is also a generator is not possible.
an iterable is an object with a Symbol.iterator property

Chapter 9 Introducing Javascript Classes

class PersonClass {
// equivalent of the PersonType constructor
constructor() {
  this.items = [];
}
*[Symbol.iterator]() {
  yield *this.items.values();
}
// equivalent of PersonType.prototype.sayName
sayName() {
  console.log(this.name);
}
// equivalent of PersonType.create
static create(name) {
  return new PersonClass(name);
}
}
let person = PersonClass.create("Nicholas");

note Static members are not accessible from instances. You must always access static members from the class directly.

Inheritance with Derived Classes

class Square extends Rectangle {
// no constructor
}
// is equivalent to
class Square extends Rectangle {
constructor(...args) {
super(...args);
}
}

notes on using super()
Keep the following key points in mind when you’re using super() :

  • You can only use super() in a derived class constructor. If you try to use it in a non derived class (a class that doesn’t use extends ) or a function, it will throw an error.
  • You must call super() before accessing this in the constructor. Because
    super() is responsible for initializing this , attempting to access this before calling super() results in an error.
  • The only way to avoid calling super() is to return an object from the class constructor.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,449评论 0 10
  • 从炉灶边到田野里,无力的诗人喝着鸦片酒,岁月无边黑暗,杯中的酒泛起阵阵涟漪,诗行不再铿锵有力了,处处都是病句,标点...
    耕天下阅读 205评论 2 0
  • 这是一盆被我救活的植物,它的花期已过,花谢了,叶子也凋零了。我给它换了个花盆,施了点肥浇了点水,没想到它又活过来了...
    颜色的主人阅读 387评论 0 2
  • 兔兔马阅读 280评论 7 3
  • 一. 新建文件HelloWorld.java 直接建一个TXT文本文档修改名字 名字(HelloWorld)需要用...
    南三号阅读 222评论 0 0