property shorthand & short methods
示例:
//Object shorthand
function getPerson() {
let name = 'John';
let age = 25;
// return {
// "name": name,
// "age": age,
// "greet": function() {
// return 'Hello ' + name;
// }
// }
return {
name,
age,
greet() {
return `Hello ${name}`;
}
}
}
console.log(getPerson().greet());
object destructuring
示例:
//Object destructuring
let data = {
name: 'John',
age: 18
}
let { name } = data;
console.log(name);
//Another Object destructing
function greet({name, age}) {
console.log(`Hello, ${name}. You are ${age}`);
}
greet({
name: 'Luke',
age: 24
});
网友评论