You can access the characters of a string directly by its index using the String.prototype.charAt method:
var str = "foo"; for (var i = 0; i < str.length; i++) { alert(str.charAt(i)); }
But I donβt think you want to traverse the character of the namespace character by character, you can use the String.prototype.split method to get an array containing the namespace levels using a dot character ( . ) As a separator, for example:
var levels = "MyCompany.UI.LoginPage".split('.'); // levels is an array: ["MyCompany", "UI", "LoginPage"]
But I think that your question goes further, and I will give you a more advanced starting point, I made a recursive function that will allow you to do exactly what you want, initialize several levels of nested objects using a line:
Application:
initializeNS('MyCompany.UI.LoginPage'); // that will create a MyCompany global object // you can use it on an object to avoid globals also var topLevel = {}; initializeNS('Foo.Bar.Baz', topLevel); // or var One = initializeNS('Two.Three.Four', {});
Implementation:
function initializeNS(ns, obj) { var global = (function () { return this;})(),
You will need to improve this function, for example, you will need to misinform the string ...
CMS
source share