2

I'm having trouble with the following piece of code. The error occurs in the last line:

return p_Function.constructor.name + "(" + v_args + ")";

When I run it on Internet Explorer 8, the function returns undefined(). Howerver, it works perfectly for Google Chrome (returning FunctionName()). I think it's a problem with the "constructor" property, but I can't figure out how to solve it. I'm new to JavaScript, and I'd be glad if I could get some help with this.

Thanks in advance.

getFunctionExecutionString: function(p_Function){
        var v_args = "";
        if(p_Function.arg) {
            for(var k=0; k < p_Function.args.length; k++) {
                if(typeof  p_Function.args[k] == "string"){
                    v_args += "\"" + p_Function.args[k].replace(/'/g, "") + "\",";
                }
                else{
                    v_args += p_Function.args[k] + ",";
                }
            }
            v_args = trim(v_args,",");
        }

               return p_Function.constructor.name + "(" + v_args + ")";
     }
  };
Bizzys
  • 141
  • 1
  • 5
  • 12
  • 2
    I don't think `constructor.name` (or rather `Function.name`) works in all browsers: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name – gen_Eric Oct 22 '13 at 21:11
  • 1
    Side Question: Do you use eval() in the next step...that looks scary. – epascarello Oct 22 '13 at 21:11
  • @epascarello, I didn't write the code, I'm just solving compatibility bugs. – Bizzys Oct 22 '13 at 21:14
  • 1
    @Bizzys: This won't work in IE. Functions don't have the `name` property. – gen_Eric Oct 22 '13 at 21:15
  • @RocketHazmat: then, how can I get the function name? I need this to work in IE. – Bizzys Oct 22 '13 at 21:19
  • 1
    @Bizzys: By finding the person who wrote `getFunctionExecutionString` and telling them their design is horrible! :-D – gen_Eric Oct 22 '13 at 21:21

1 Answers1

4

Per How do I get the name of an object's type in JavaScript?

return p_Function.constructor.toString().match(/function (.{1,})\(/)[1] + "(" + v_args + ")";

Example:

var A = function A(){};
var a = new A();
console.log(a.constructor.toString().match(/function (.{1,})\(/)[1]);
Community
  • 1
  • 1
zamnuts
  • 9,492
  • 3
  • 39
  • 46