a可能是这样
var a = {
result: {
item: [1,2]
}
}
也可能是这样
var a = {
result: []
}
也可能是这样
var a = {
other: []
}
我要封装根据字符串来找到a对应key的value值
// 这个str传有可能是'result', 'result.items', 'other'
function test (str) {
// a[str] ???
}
// 那么我这里这么写才能使得上面的object a['变量']解析出来呢?
如果传入的参数约定好,获取内部的值时,属性是按从外往内,用
.
连接起来的字符串。那么就在test()内部,把参数str拆分开,从外向内依次寻找即可:执行: