Home >Web Front-end >Front-end Q&A >What is the difference between es6 map objects and native objects?

What is the difference between es6 map objects and native objects?

WBOY
WBOYOriginal
2022-05-06 17:14:161870browse

Difference: 1. The type of the key in the native object object's key-value pair combination is string, and the map object's key-value pair storage type can be any type; 2. The native object object uses the key-value combination "Object.keys" returns an array, while the map object uses "map variable.keys()".

What is the difference between es6 map objects and native objects?

The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.

What is the difference between the map object of es6 and the native object?

Difference

Object and Map store both key-value pair combinations. However: the key type of

  • object is a string; the key type of

  • map can be any type;

Also note that

  • object obtains key values ​​using Object.keys (returns array);

  • Map obtains Key values ​​use map variable.keys() (returns iterator).

Sample code:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>js es6 map 与 原生对象区别</title>
    </head>
    <body>
        <script type="text/javascript">
            let a = {
                o: 1
            };
            // string
            console.log(typeof Object.keys(a)[0]);
            let map = new Map();
            map.set(a, &#39;content&#39;);
            // 输出是object 也可以是任何类型
            console.log(map.keys().next());
        </script>
    </body>
</html>

[Related recommendations: javascript video tutorial, web front-end]

The above is the detailed content of What is the difference between es6 map objects and native objects?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn