0

i have a namespace object form.iso.methodName as a string and i want to call that method. I cant create namespace object like [form][iso][methodName]

split : function(ns) { // ns namespace is object
    var splitNameSpace = ns.split('.');
    var methodName = splitNameSpace.pop();
    //console.log(splitNameSpace);
    var content = [];

    for (var i=0;i<splitNameSpace.length;i++){
        content = [splitNameSpace[i]];
        console.log(content);
    }

*Update- Found solution *

split : function(ns) {
    var splitNameSpace = ns.split('.');
    var methodName = splitNameSpace.pop();
    //console.log(splitNameSpace);
    var content = [];
    var obj;
    for (var i=0;i<splitNameSpace.length;i++){
        obj = [splitNameSpace[i]];
    content = content+"["+obj+"]";

    }
7
  • Could you try to explain a bit better what you want to do? Commented Mar 2, 2013 at 2:57
  • @AndrewMcGivery i am passing a namespace string in function split parameter and i want that function to split up and build namespace object [form][iso[methodName] so i can call a function which is in different namespace object.. i hope i explained .i am bad at english Commented Mar 2, 2013 at 3:00
  • So, if I'm to understand, you want to call form.iso.methodName() given the string "form.iso.methodName"? Commented Mar 2, 2013 at 3:03
  • @AndrewMcGivery yes but the form.iso is namespace object Commented Mar 2, 2013 at 3:04
  • Couldn't you just call eval(ns+'()') ? Commented Mar 2, 2013 at 4:04

1 Answer 1

2

You can do this:

function namespace(ns){
    var d, o;
    n = ns.split('.');
    o = window[n[0]] = window[n[0]] || {};
    var l = n.length;
    for(var i=1; i<l; i++){
        o = o[n[i]] = o[n[i]] || {};
    }
    return o;
}

 var param = 'foo.bar.blah.methodName',
     method = param.split('.').pop(),
     ns = param.split('.').slice(0,-1).join('.'),
     obj = namespace(ns); 

 obj[method]();

You can improve this by retrieving the global object instead of using window

Sign up to request clarification or add additional context in comments.

4 Comments

i cant get it and i am trying to pop the last ns after split for method name and the rest as namespace object but i dont understand what you are doing?
You can call namespace to retrieve the namespace of your method: var param = 'foo.bar.blah.methodName'; method = ns.split('.').pop(); ns = param.split('.').slice(0,-1).join('.'); obj = namespace(ns); obj[method]();
i found out the solution bit untidy but works split : function(ns) { var splitNameSpace = ns.split('.'); var methodName = splitNameSpace.pop(); //console.log(splitNameSpace); var content = []; var obj; for (var i=0;i<splitNameSpace.length;i++){ obj = [splitNameSpace[i]]; //console.log(obj); content = content+"["+obj+"]"; //var cont = content[splitNameSpace[i]]; //content = cont; //cont = content[i]; //console.log(content); //console.log(methodName); }
wow - exactly what i needed and it works! One comment: in function namespace the variable d is not used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.