generic 2 fixes i.e. more reliable solution
initial! even every thing is like a string !!!
From the above problem, I am extending objectname, propertyname is a Jang string! ...
objectname = "obj";
propertyname = "start";
// try the notification (get (objectname + "." + test + "." + property name));
function get(path) {
var next = window;
path = path.split(/[\[\]\.]+/);
if (path[path.length - 1] == "") {
path.pop();
};
while (path.length && (next = next[path.shift()]) && typeof next === "object" && next !== null);
return path.length ? undefined : next;
}
Other
function getPropertyValueByPath (obj, path)
{
path = path.split(/[\[\]\.]+/);
if(path[path.length - 1] == "")
{
path.pop();
};
while(path.length && ( obj = obj[path.shift()]));
return obj;
}
Using
alert(getPropertyValueByPath(obj,test+ "." + propertyname ));
alert(get(objectname + "." + test + "." +propertyname));
Non recommented way
eval(objectname + "." + test + "." +propertyname )
Another way `eval("obj." + test + ".start")`
a way of insecure and non-advised eval()
source
share