对象语法增强
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>对象字面量增强写法</title>
</head>
<body>
<script>
const nameKey = 'name';
const ageKey = 'age';
const heightKey = 'height';
const sayNameKey = 'sayName';
let person = {
[nameKey]: 'LQJ',
[ageKey]: 18,
[heightKey]: 1.88
};
console.log(person);
const name = 'TTY';
const age = 25;
const height = 1.63;
let person1 = {
name,
age,
height
};
console.log(person1);
let person2 = {
sayName: function () {
console.log('我是es5的函数定义');
}
};
person2.sayName();
let person3 = {
sayName() {
console.log('我是es6的函数定义');
}
};
person3.sayName();
let person4 = {
[sayNameKey]() {
console.log('我是es6的函数定义');
}
};
person4.sayName();
</script>
</body>
</html>