一、object对象
- 创建object对象
const jonas = {
firstName: 'Jonas',
lastName: 'Schmedtmann',
age: 2057 - 1991,
job: 'teacher',
friends: ['Michael', 'Peter', 'Steven']
};
- 引用object对象
console.log(jonas);
// 第一种:点引用对象具体内容
console.log(jonas.lastName);
console.log(jonas['lastName']);
// 第二种:[]引用对象具体内容
const nameKey = 'Name';
console.log(jonas['first' + nameKey]);
console.log(jonas['last' + nameKey]);
// 计算属性名用[]表达法
const interestedIn = prompt('What do you want to know about Jonas? Choose between firstName, lastName, age, job, and friends');
if (jonas[interestedIn]) {
console.log(jonas[interestedIn]);
} else {
alert("Wrong request! Choose between firstName, lastName, age, job, and friends");
}
// 也可以直接添加属性
jonas.location = 'China';
jonas['tiwwer'] = '@sssss';
console.log(jonas);
原创文章,作者:6024010,如若转载,请注明出处:https://blog.ytso.com/272404.html