質問: 'bar'; something.foo; // the idea is to access something.bar, getting "foobar!"
回答: 例えば :a = [ {b:[{a:1,b:[{c:1,d:2}]}]} ] の代わりに :if(a && a[0] && a[0].b && a[0].b[0] && a[0].b[0].b && a[0].b[0].b[0] && a[0].b[0].b[0].d && a[0].b[0].b[0].d == 2 ) // true 私たちは今できる:if( getvalue('a[0].b[0].b[0].d') == 2 ) // true ここにコードがあります/** * @method getvalue * @description simplifies checking for existance and getting a deeply nested value within a ceratin context * @argument {string} s string representation of the full path to the requested property * @argument {object} context optional - the context to check defaults to window * @returns the value if valid and set, returns undefined if invalid / not available etc. */ var getvalue = function( s, context ){ var fn = function(){ try{ return eval(s); }catch(e){ return undefined; } } return fn.call(context||window,s); }