objects of non-ES6 map map array () method
Popular interpretation map is used to store things, similar to the obj
Writing
For example:
var map = new Map(); map.a = 1 map.b = 2 console.log(map)
And then look at the array, the same property added
var arr = []; arr.a = 1 arr.b = 2 console.log (arr)
Both are very similar
Conclude: map objects, as the new reference value out, RBI by way of adding attribute, length is 0, indicating that the attribute is added directly to the map object up, but has not actually stored values,
Can be understood as, add RBI, just add a property to map objects, but not as a sub-element map object
Additions and deletions to change search object on map
var map = new Map(); map.set("a","123"); map.set("b","456"); //增与改 console.log(map) console.log(map.get("a")) //查
delete
var map = new Map(); map.a = 1 map.set("a","123"); map.set("b","456") map.delete("a") //删 delete map.a //删属性 console.log(map)
Traversing the map object
This object is looped over using a for loop is definitely not feasible
Try for in
var map = new Map(); map.a = 1 map.set("a","123"); map.set("b","456") for(var key in map){ console.log(key) //a console.log(map[key]) //1 }
The results are expressed using the for in loop to iterate over the properties or very good use
It can be used for of the new ES6
for of cyclic object, then the browser will tell you that the object can not be repeated
You can iterate
But the main object is used to circulate map
as follows:
var map = new Map(); map.a = 1 map.set("a","123"); map.set("b","456") for(var key of map){ console.log(key) //["a", "123"] ["b", "456"] }
key value is returned in the form of an array
Since the key value is an array, it has a new play:
var map = new Map(); map.a = 1 map.set("a","123"); map.set("b","456") for(var [key,value] of map.entries()){ console.log(key) //a b console.log(value) //123 456 }
Added: Full write map.entries () to map objects
If you just want to traverse the value of it?
map换map.values()
Play as follows:
var map = new Map(); map.a = 1 map.set("a","123"); map.set("b","456") for(var val of map.values()){ console.log(val) //123 456 }
If you just want to traverse the key it?
map change map.keys ()
as play:
var map = new Map(); map.a = 1 map.set("a","123"); map.set("b","456") for(var key of map.keys()){ console.log(key) //a b }
calss (class)
Traditional javascript, only objects, there is no concept of class. It is based on the prototype of an object-oriented language. The prototype object is characterized by its property to the new shared object.
Person {class // define a name for the Person class constructor (name, Age) { // constructor is configured for receiving parameters of the this .name = name; // the this instance object represents the this .age = Age ; } say () { // this is a class method, do not pay attention plus function return , "my name is" + the this .name + "this year" + the this .age + "years old" ; } }
let p1 = new Person ( "jack ", 18) // create an instance of an object
console.log (p1.say ()) // My name is jack 18 years old this year, the
note:
1. Class declaration does not need to add function
2. The method is not in the constructor
inherit
By extends to inherit
Person {class // define a name for the Person class constructor (name, Age) { // constructor is a constructor parameter for receiving the this .name = name; // the this instance object represents the this .age = Age; } say () {// class methods return "my name is" + the this .name + "this year" + the this .age + "years old" ; } } the let p1 = new new the Person ( "Jack", 18 ) class the extends the Person {} Son the let Son = new new Son ( "Tony", 20 is ) the console.log (son.name) // Tony
Inheritance Example 2:
{the Person class constructor (name, Age) { the this .name = name; the this .age = Age; } say () { return "My name is" + the this .name + "this year" + the this .age + "years old" ; } } the let P1 = new new the Person ( "Jack", 18 is ) class the extends the Person {Son constructor (name, Age, Color) { Super (); // write inheritance must be added, and this was applied on top of this = .name name; the this .age = Age; the this .color = Color;
} } The let Son = new new Son ( "Tony", 20, "Black" ) console.log (son.name) // Tony console.log (son.say ()) // My name is tony 20 years old this year, the