javascript - how to get a property name which represent a function in JS -


this js code

var methodarr = ['firstfunc','secondfunc','thirdfunc'];  for(var in methodarr) {      window[methodname] = function()     {         console.log(methodname);     } } 

my problem how name of function in js.

in js, use this.callee.name.tostring() can function name. in situation, null value. how can 'funname' string?

sorry, didn't make clear. want create functions in loop, these functions has same implementation need name. others can call these functions use different name.i want know methodname function called.

it seems scope problem.

try this:

var methodarr = ['firstfunc','secondfunc','thirdfunc']; for(var in methodarr) {     var methodname = methodarr[i]; // <---- line missed in code?      window[methodname] = (function(methodname) {         return function() {             console.log(methodname);         }     })(methodname); }  window['secondfunc'](); // output: secondfunc 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -