12.1?使用?.
?來(lái)訪問(wèn)對(duì)象的屬性。
const luke = {
jedi: true,
age: 28,
};
// bad
const isJedi = luke['jedi'];
// good
const isJedi = luke.jedi;
12.2?當(dāng)通過(guò)變量訪問(wèn)屬性時(shí)使用中括號(hào)?[]
。
const luke = {
jedi: true,
age: 28,
};
function getProp(prop) {
return luke[prop];
}
const isJedi = getProp('jedi');
更多建議: