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.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,233评论 6 495
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,357评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,831评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,313评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,417评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,470评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,482评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,265评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,708评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,997评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,176评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,827评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,503评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,150评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,391评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,034评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,063评论 2 352

推荐阅读更多精彩内容

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