const jonas = { firstName: 'Jonas', lastName: 'Schmedtmann', age: 2057 - 1991, job: 'teacher', friends: ['Michael', 'Peter', 'Steven'] };
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);