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
Post a Comment