In Javascript, Is There Syntatic Shortcut Checking Existence Of Each Layer Of Embedded Object?
Solution 1:
there's no real shortcut. You can write a helper function to do it for you, something that can condense to:
function getProp(obj){
var i=0, l=arguments.length;
while(i<l && (obj = obj[arguments[i++]]));
return obj;
}
if( getProp(obj, 'attr1', 'attr2', 'attr3', 'attr4') == 'constant')
or you can do:
var tmp;
if((tmp = obj.attr1)
&& (tmp=tmp.attr2)
&& (tmp=tmp.attr3)
&& (tmp.attr4 == 'constant')) {
Solution 2:
As people have mentioned, but nobody has done, you can use try/catch:
try {
if(obj.attr1.attr2.attr3.attr4 == 'constant') return;
} catch(e) {}
It's not the best code ever, but it's the most concise, and easily readable. The best way of avoiding it would be not to have so deeply nested a tree of possibly-absent objects.
Solution 3:
Interesting question - though I've never had the problem myself. The best alternative I can think of is to write a helper function:
function get(chain, context) {
var o = arguments.length == 2 ? context : window,
c = chain.split('.');
for (var i = 0; i < c.length; i++) {
if (!o) return null;
o = o[c[i]];
}
return o;
}
If obj
is global then you can do something like:
if (get('obj.attr1.attr2.attr3.attr4') == 'constant') return;
Otherwise:
if (get('attr1.attr2.attr3.attr4', obj) == 'constant') return;
Solution 4:
No, unfortunately, there isn't, short of using try/catch
. You could, though, write yourself a helper function (untested, but the concept is there):
function existsAndEquals(obj, layers, compare) {
for (var i = 0; i < layers.length; i++)
if (!(obj = obj[layers[i]]))
return false;
return obj == compare;
}
if (existsAndEquals(obj, ['attr1', 'attr2', 'attr3', 'attr4'], 'constant'))
// ...
Solution 5:
You can use call (or apply) to shift the context of a string evaluation from the window to any object
function getThis(string){
var N= string.split('.'), O= this[N.shift()];
while(O && N.length) O= O[N.shift()];
return O;
}
window.obj={
attr1:{
attr2:{
attr3:{
attr4: 'constant!'
}
}
}
}
getThis('obj.attr1.attr2.attr3.attr4');
/* returned value: (String) 'constant!' */
getThis.call(obj, 'attr1.attr2.attr3.attr4');
/* returned value: (String) 'constant!' */
Post a Comment for "In Javascript, Is There Syntatic Shortcut Checking Existence Of Each Layer Of Embedded Object?"