This series primarily records JavaScript, where beginners are more likely to be mistaken.
(1) JavaScript objects
Since JavaScript objects are dynamic types, you are free to add or remove properties to an object: var ;
Undefined
= 18; Add an Age Property
(2)JavaScript treats null, undefined, 0, Nan, and empty string ' ' as false, and all other values are treated as true.
(3) Map
Map is the structure of a set of key-value pairs with extremely fast lookup speeds .
For example, suppose you want to find the corresponding score according to the name of the classmate, if you use an array, you need two arrays:
var names = [' Michael ', ' Bob ', ' Tracy '];
var scores = [95, 75, 85];
Given a name, to find the corresponding score, first to find the corresponding position in the names, and then remove the corresponding results from scores, the longer the array, the longer the time.
If you use map to achieve, only need a "name"-"score" of the table, directly based on the name of the results, no matter how large the table, the search speed will not be slow. Write a map with JavaScript as follows:
var m = new Map ([' Michael ', ' ['], ' [' Bob ', ' [']], [' Tracy ', 85]]);
M.get (' Michael '); 95
Initializing a map requires a two-dimensional array, or directly initializing an empty map. Map has the following methods:
var m = new Map (); Empty map
M.set (' Adam ', 67); Add a new Key-value
M.set (' Bob ', 59);
M.has (' Adam '); Is there a key ' Adam ': true
M.get (' Adam '); 67
M.delete (' Adam '); Delete key ' Adam '
M.get (' Adam '); Undefined
Since a key can only correspond to one value, the value is placed multiple times for a key, and the following values will flush the previous value out :
var m = new Map ();
M.set (' Adam ', 67);
M.set (' Adam ', 88);
M.get (' Adam '); 88
(4) Iterable
Traversing an array can take the subscript loop, and traversing the map and set cannot use the subscript. To unify the collection type, the ES6 standard introduces a new iterable type, with Array, map, and set belonging to the iterable type.
A collection with the iterable type can traverse through a new for ... of loop, but requires your browser support. Iterate through the collection with the for ... of, with the following usage:
var a = [' A ', ' B ', ' C '];
var s = new Set ([' A ', ' B ', ' C ']);
var m = new Map ([[1, ' X '], [2, ' Y '], [3, ' Z ']]);
for (var x of a) {//traversal array
alert (x);
}
for (var x of s) {//Traversal set
alert (x);
}
for (Var x of M) {//Traverse map
Alert (x[0] + ' = ' + x[1]);
}
(5)What is the difference between the for ... of loops and for ... in loops?
The For ... in loop is actually a property name for an object because of a history legacy problem. An array is actually an object, and the index of each element is treated as an attribute.
When we manually add extra attributes to the array object, the for ... in loop brings unexpected unintended effects:
var a = [' A ', ' B ', ' C '];
A.name = ' Hello ';
for (Var x in a) {
alert (x); ' 0 ', ' 1 ', ' 2 ', ' name '
}
The for ... in loop will include name, but the length property of the array is not included.
The for ... of loop completely fixes these problems, it only loops the elements of the collection itself:
var a = [' A ', ' B ', ' C '];
A.name = ' Hello ';
for (var x of a) {
alert (x); ' A ', ' B ', ' C '
}
This is why you should introduce a new for ... cycle.
However, the better way isusing the iterable built-in foreach method directly, it receives a function,The function is automatically recalled every iteration。 Take array for example:
var a = [' A ', ' B ', ' C '];
A.foreach (function (element, index, array) {
Element: A value that points to the current element
Index: point to Current index
Array: points to the array object itself
alert (element);
});
Note that the ForEach () method is introduced in the ES5.1 standard and you need to test whether the browser supports it.
Set is similar to array, but set has no index, so the first two parameters of a callback function are the elements themselves:
var s = new Set ([' A ', ' B ', ' C ']);
S.foreach (function (element, sameelement, set) {
alert (element);
});
The callback function parameters for map are value, key, and map itself:
var m = new Map ([[1, ' X '], [2, ' Y '], [3, ' Z ']]);
M.foreach (function (value, key, map) {
alert (value);
});
If you are not interested in certain parameters, they can be ignored because JavaScript function calls do not require parameters to be consistent. For example, you only need to get an element of array:
var a = [' A ', ' B ', ' C '];
A.foreach (function (Element) {
alert (element);
});
JavaScript Basics Collection (iii)