resolve("document.body.style.width")
// or
resolve("style.width", document.body)
// or even use array indexes
// (someObject has been defined in the question)
resolve("part.0.size", someObject)
// returns null when intermediate properties are not defined:
resolve('properties.that.do.not.exist', {hello:'world'})
var something = {
bar: 'foo'
};
var foo = 'bar';
// both x = something[foo] and something[foo] = x work as expected
console.log(something[foo]);
console.log(something.bar)
這是我的解決方案:
使用範例:
有兩種存取屬性的方法 物件:
something.bar
something['bar']
#括號內的值可以是任意表達式。因此,如果屬性名稱儲存在變數中,則必須使用括號表示法: