05Symbol
作者:互联网
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>05Symbol</title>
</head>
<body>
<script>
/*5.1 基本使用*/
// Symbol是一个永远不会重复的字符串
// let name = Symbol("这是一个描述");
// console.log(typeof name); // symbol
// console.log(name); // Symbol(这是一个描述)
// console.log(name.description); // 这是一个描述
// let name1 = Symbol("这是一个描述");
// console.log(name === name1); // false
/*5.2 使用Symbol解决字符串耦合问题*/
let user1 = {
name : "李四",
key: Symbol()
};
let user2 = {
name : "李四",
key: Symbol()
};
let grade = {
[user1.key]: {js : 100, css: 89},
[user2.key]: {js : 35, css: 55}
};
console.log(grade);
/*
{Symbol(): {…}, Symbol(): {…}}
Symbol(): {js: 100, css: 89}
Symbol(): {js: 35, css: 55}
*/
console.log(grade[user1.key]); //{js: 100, css: 89}
console.log(grade[user2.key]); //{js: 35, css: 55}
</script>
</body>
</html>
标签:console,name,Symbol,js,05Symbol,css,log 来源: https://www.cnblogs.com/LiuChang-blog/p/16485255.html