因爲null
和undefined
不是JavaScript中的對象,我猜它可能是全局幫助函數?Rails對所有對象都有一個「try」方法。有沒有辦法在JavaScript中做類似的事情?
用法示例可能是這樣的:
var a = { b: { c: { d: 'hello world!' } } };
tryPath(a, 'b', 'c', 'd'); // returns 'hello world!'
tryPath(a, 'x', 'c', 'd'); // returns undefined
po可能的重複[可能忽略無法讀取屬性'0'的未定義?](http://stackoverflow.com/questions/22145087/possible-to-ignore-cannot-read-property-0-of-undefined) –