Professional JavaScript for Web Developers 第四版學(xué)習(xí)筆記 CHAPTER 6: COLLECTION REFERENCE TYPES
The Object Type 167
The Array Type 170
Creating Arrays 170
Array Holes 172
Indexing into Arrays 174
Detecting Arrays 175
Iterator Methods 176
Copy and Fill Methods 176
Conversion Methods 179
Stack Methods 180
Queue Methods 181
Reordering Methods 182
Manipulation Methods 184
Search and Location Methods 186
Strict Equivalence 186
Predicate Search 187
Iterative Methods 188
Reduction Methods 189
Typed Arrays 190
History 190
WebGL 190
Emergence of Typed Arrays 191
Using ArrayBuffers 191
DataViews 192
ElementType 193
Big-Endian and Little-Endian 194
Corner Cases 195
Typed Arrays 196
Typed Array Behavior 197
Merging, Copying, and Changing Typed Arrays 198
Underflow and Overflow 200
The Map Type 201
Basic API 201
Order and Iteration 203
Choosing Between Objects and Maps 206
Memory Profile 206
Insertion Performance 206
Lookup Performance 206
Delete Performance 206
The WeakMap Type 206
Basic API 207
Weak Keys 208
Non-Iterable Keys 209
Utility 209
Private Variables 209
DOM Node Metadata 211
The Set Type 211
Basic API 211
Order and Iteration 213
Defining Formal Set Operations 215
The WeakSet Type 217
Basic API 217
Weak Keys 219
Non-Iterable Values 219
Utility 219
Iteration and Spread Operators 220
Summary 222
-------------------------------------------------
object literals
object literals have become a preferred way of passing a large number of optional arguments to a function, such as in this example:
function displayInfo(args) {
let output = "";
if (typeof args.name == "string"){
output += "Name: " + args.name + "\n";
}
if (typeof args.age == "number") {
output += "Age: " + args.age + "\n";
}
alert(output);
}
displayInfo({
name: "Nicholas",
age: 29
});
displayInfo({
name: "Greg"
});
-------------------------------------------------
from() and of()
The Array constructor also has two additional static methods introduced in ES6 to create arrays: from() and of(). from() is used for converting array-like constructs into an array instance, whereas of() is used to convert a collection of arguments into an array instance.
const a1 = [1, 2, 3, 4];
const a2 = Array.from(a1, x => x**2);
const a3 = Array.from(a1, function(x) {return x**this.exponent}, {exponent: 2});
alert(a2); // [1, 4, 9, 16]
alert(a3); // [1, 4, 9, 16]
alert(Array.of(1, 2, 3, 4)); // [1, 2, 3, 4]
alert(Array.of(undefined)); // [undefined]
-------------------------------------------------
every(),filter(),forEach(),map(),some()
Each of the methods accepts two arguments: a function to run on each item and an optional scope object in which to run the function (affecting the value ofthis). The function passed into one of these methods will receive three arguments: the array item value, the position of the item in the array, and the array object itself.
These methods do not change the values contained in the array.
let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
let everyResult = numbers.every((item, index, array) => item > 2);
alert(everyResult); // false
let someResult = numbers.some((item, index, array) => item > 2);
alert(someResult); // true
let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
let filterResult = numbers.filter((item, index, array) => item > 2);
alert(filterResult); // [3,4,5,4,3]
let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
let mapResult = numbers.map((item, index, array) => item * 2);
alert(mapResult); // [2,4,6,8,10,8,6,4,2]
let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
numbers.forEach((item, index, array) => {
// do something here
});
-------------------------------------------------
reduce() and reduceRight()
let values = [1, 2, 3, 4, 5];
let sum = values.reduce((prev, cur, index, array) => prev + cur);
alert(sum); // 15