美文网首页
understanding es6

understanding es6

作者: onbug | 来源:发表于2018-04-28 21:22 被阅读0次

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.

相关文章

网友评论

      本文标题:understanding es6

      本文链接:https://www.haomeiwen.com/subject/muuglftx.html