1

I have following string which needs to be parsed using JS.

function () {\n     MyClass.prototype.doSomethingFunction();\n       5-6 lines of coding\n  }

I tried parsing it, trimming whitespaces and newlines but nothing worked for me. Please let me know how can I get function name (doSomethingFunction) from the above string.

How I'm getting this string:

I have a queue where my functions are stored. Later in time my code picks a function from this queue (some logic here) and execute them. It works perfectly ok for me. But I just want to print the name of the function out of it! It's like '(classOBJ.myFunctionsQueue[n])()' is used to execute a function which is stored at nth location in myFunctionsQueue array. Make sense or I'm doing something wrong in here?

Thanks MANN

8
  • Why do you need to parse JavaScript within JavaScript? Commented Oct 11, 2012 at 14:31
  • 2
    Are you mean to get a name of function who has no name? Commented Oct 11, 2012 at 14:31
  • Can you not populate your queue with the function name as well as a separate property? i.e., ["doSomethingFunction", "function () {\n... "]. Commented Oct 11, 2012 at 14:32
  • MyClass.prototype.doSomethingFunction() is the whole namespace. I just want doSomethingFunction() out of it. Does this make sense? Commented Oct 11, 2012 at 14:33
  • 2
    Your solution sounds very fragile. What is the bigger picture goal you are aiming for, perhaps there is a better approach overall? Commented Oct 11, 2012 at 14:33

3 Answers 3

2

Try a reg expression matching like this

var str = "function() {\n MyClass.prototype.doSomethingFunction();\n 5 - 6 lines of coding\n}";

var matches = str.match(/prototype\.(.+?)\(\)/);
if(matches){
    alert(matches[1]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

gr8... thats the thing I was looking for. Thanks a lot Diode. BTW using array to store your function is a good design or bad? From the comments above it sounds like I'm doing something evil!!!
@MANN: An array seems to be reasonable for a queue, but storing your functions as strings is evil.
0

I don't think it's the right approach for your application, but the current problem could be solved by something like

myFunctionString.split("\n").reduce(function(map, line) {
    if (/^\s*[^\s[\]]+\(\);?\s*$/.test(line))
        map.push(line.split(".").pop().replace(/\(\);?\s*$/, ""));
    return map;
}, []);
// returns an array of invoked method names

2 Comments

what I'm doing : var myObj = new MYClass(); var queueOBJ = new QUEUECLASS(few param); queueOBJ.addTest(myObj.doSomethingFunction); Is this design alright or ... even @bfavaretto thinks its alright. Would you mind telling me what I'm doing wrong here?
You can't really rely on some code in a string, it might too easily change to something non-parsable. Also I don't understand your use case entirely, why would you want to get the property name of the first method called inside some function string?
0

First, if you're executing functions stored as strings, you're probably doing it wrong (using eval or new Function(body)). Couldn't you use an object as the queue, with the function names as keys, and function references (not strings) as values? Like this:

var queue = {
    "funcName" : function() { /* do something */ },
    "otherFunc" : function() { /* do something else */ }
    /* etc. */
};

Then you can print all names an execute all function from a loop, for example:

for(var key in queue) {
    console.log(key); // function name
    queue[key]();     // execute function
}

2 Comments

I doubt that I'm storing my functions as string. Let me tell you what I'm doing : var myObj = new MYClass(); var queueOBJ = new QUEUECLASS(few param); queueOBJ.addTest(myObj.doSomethingFunction); Is this design alright or ...
That looks fine. You said it was a string and needed parsing, hence my answer.

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.