you shuld try the following function which I wrote about the time when AJAX came out and it works with hastables well formed namespace hierarchies as in (continue reading)
Introducing seamless guesswork in JavaScript
Determine if a namespace/variable-name exists.
Check for namespace existence in your environment variables and objects before writing to them.
Guess if "Book1.chapter22.paragraph37" exists without provoking errors and retrieve/send data if it exists with the simplest expression :
Call syntax:
isNS( [string], [num] )
Example:
var nsresult =
isNS( "Book1.chapter22.paragraph37", -1 );
if( nsresult[0] ){ send( nsresult[1] ) else notify( nsresult[4], nsresult[2] ) };
etc etc
// omit modifier or 0, false and undefined to get a Boolean
isNS( "myObject.property.subProperty.myMethod" );
_> Boolean : true/false
Modifier Possible values:
_> -1; (0|false|undefined); ( 1|true ); 2; 3 or 4.
Description:
(-1 ) _> returns array object containing complete inoculation.
( 0 ) _> optional; returns Boolean: true (if complete NS chain exist) : false (if it's broken).
( 1 ) _> if NS* query exists, case true: returns the existing value; if not, case false: returns the last valid context if any, or the global object if root doesn't exist.
( 2 ) _> returns an array of existing namespaces in the argument query.
( 3 ) _> returns an array containing all specified namespaces.
( 4 ) _> returns the original query string.
function isNS(arg,f) { /*b.b. Troy III p.a.e.*/
var i, a = arg.split("."), c = this, s = [], b, r;
f = f || 0;
for( i in a ) {
c ? a[i] in c ? ( c = c[ a[i] ], s.push( a[i] ), b = !0 ) : b = !1 : 0;
}
r = [ b, c, s, a, arg ];
return f < 0 ? r : r[+f||f]
}
*NS - stands for already familiar "Namespace" initials.
[This makes but a tinny cluster on some real A.I. yet -very important one!]
;
function isNS(arg,f) { /*b.b. Troy III p.a.e.*/
var i, a = arg.split("."), c = this, s = [], b, r;
f = f || 0;
for( i in a ) {
c ? a[i] in c ? ( c = c[ a[i] ], s.push( a[i] ), b = !0 ) : b = !1 : 0;
}
r = [ b, c, s, a, arg ];
return f < 0 ? r : r[+f||f]
}
;
book1 = {
chapter1 : {
paragraph1 : ["first line","second line"],
paragraph2 : ["first line","second line"],
paragraph3 : ["first line","second line"] },
chapter2 : {
paragraph1 : ["first line","second line"],
paragraph2 : ["first line","second line"],
paragraph3 : ["first line","second line"] },
chapter3 : {
paragraph1 : ["first line","second line"],
paragraph2 : ["first line","second line"],
paragraph3 : ["first line","second line"] }
}
;
console.log( 'Boolean for:"book1.chapter2.paragraph3"' )
;
console.log( isNS("book1.chapter2.paragraph3", 0 ) )
;
console.log( 'Retrieve:"book1.chapter2.paragraph3"' )
;
console.log( isNS("book1.chapter2.paragraph3", 1 ) )
;