Javascript 如何命名一个函数,以便您可以对其进行内省?
我想编写一个例程,在调用时将函数名附加到#trace:Javascript 如何命名一个函数,以便您可以对其进行内省?,javascript,Javascript,我想编写一个例程,在调用时将函数名附加到#trace: ;(function($, window, undefined) { var dom = {}; var myObject = {}; myObject.myFunction = { trace('myObject.myFunction'); // function logic goes here } dom.trace = $('#trace'); var
;(function($, window, undefined) {
var dom = {};
var myObject = {};
myObject.myFunction = {
trace('myObject.myFunction');
// function logic goes here
}
dom.trace = $('#trace');
var trace = function(value) {
dom.trace.append(value + '<br>'));
}
$(document).on('click','#Save',myObject.myFunction)
})(jQuery, window);
;(函数($,窗口,未定义){
var dom={};
var myObject={};
myObject.myFunction={
跟踪(“myObject.myFunction”);
//函数逻辑在这里
}
dom.trace=$('#trace');
变量跟踪=函数(值){
append(value+'
');
}
$(文档).on('单击','保存',myObject.myFunction)
})(jQuery,窗口);
在我收集的这个小小的概念证明中,我知道我可能做错了12件事
但我的问题是:
问:如何命名一个函数以便对其进行内省?来源:
功能演示道具(a、b、c、d、e)
{
var msg=“function name=“+arguments.callee.name+”\n”;
msg+=“调用自=”+show_props.caller.name+“\n”;
msg+=“arguments.length=“+arguments.length+”参数已传递。\n
msg+=“show_props.length(arity)=“+show_props.length+”参数定义总数。\n”;
msg+=“arguments=“+arguments+”\n”;
for(var i=0;i
什么是内省的?为什么不直接使用调试器呢?可以使用参数获取函数名。被调用者,但它正在逐步淘汰(在严格模式下不起作用,在将来的ECMAScript版本中可能不起作用)。“在严格模式下不起作用”。噢。这可能就是我一直在想的。@jfriend00,我正在考虑在字段中保留一个跟踪,以帮助我重新创建用户在出现错误之前采取的步骤。@jfriend00,内省是我不必在每个函数中硬编码函数名称的地方。我只想使用c所有跟踪(参数)和函数跟踪(值)计算它来自哪个函数。
function show_props(a,b,c,d,e)
{
var msg = "function name = " + arguments.callee.name + "\n";
msg += "called from = " + show_props.caller.name + "\n";
msg += "arguments.length = " + arguments.length + " argument(s) were passed.\n"
msg += "show_props.length (arity) = " + show_props.length + " argument(s) are defined total.\n";
msg += "arguments = " + arguments + "\n";
for (var i = 0; i < arguments.length; i++)
msg += "arguments[" + i + "] = " + arguments[i] + "\n";
msg += "And arguments.callee.toString() is the function's literal body in string format = \n" + arguments.callee.toString() + "\n";
alert(msg);
}
function parent()
{
show_props(1,2,3);
}
parent();
The result is shown below:
function name = show_props
called from = parent
arguments.length = 3 argument(s) were passed.
show_props.length (arity) = 5 argument(s) are defined total.
arguments = [object Arguments]
arguments[0] = 1
arguments[1] = 2
arguments[2] = 3
And arguments.callee.toString() is the function's literal body in string format =
function show_props(a,b,c,d,e)
{
var msg = "function name = " + arguments.callee.name + "\n";
msg += "called from = " + show_props.caller.name + "\n";
msg += "arguments.length = " + arguments.length + " argument(s) were passed.\n"
msg += "show_props.length (arity) = " + show_props.length + " argument(s) are defined total.\n";
msg += "arguments = " + arguments + "\n";
for (var i = 0; i < arguments.length; i++)
msg += "arguments[" + i + "] = " + arguments[i] + "\n";
msg += "And arguments.callee.toString() is the function's literal body in string format = \n" + arguments.callee.toString() + "\n";
alert("msg");