ES6-Symbol创建对象属性

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Symbol创建对象属性</title>
</head>
<body>
<script>
// 向对象中添加 up down
let game = {

}
//存在一定的风险 不知道game中有没有up down
// game.up=function(){

// }

// 声明一个对象
let methods = {
up:Symbol(),
down:Symbol()
};
game[methods.up]=function(){
console.log('我可以改变形状');
}
game[methods.down]=function(){
console.log('我可以快速下降');
}
console.log(game);
</script>

<!-- 方法二 -->
<script>
let Yx = {
name:"小",
[Symbol('say')]:function(){
console.log('快速下降');
},
[Symbol('zi')]:function(){
console.log('快速');
}
}
console.log(Yx);
</script>
</body>
</html>

posted @ 2020-08-09 21:34  Smile*^  阅读(322)  评论(0编辑  收藏  举报