gpt4 book ai didi

javascript - 如何命名一个函数以便您可以内省(introspection)它?

转载 作者:行者123 更新时间:2023-11-28 09:14:38 25 4
gpt4 key购买 nike

我想编写一个例程,每当调用函数时将其名称附加到#trace:

;(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);

在我整理的这个小小的概念验证中,我知道我可能做错了 12 件事。

但这就是我的问题的要点:

问:如何命名函数以便对其进行内省(introspection)?

最佳答案

来自http://www.learnjquery.org/tutorials/ :

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");

关于javascript - 如何命名一个函数以便您可以内省(introspection)它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15818830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com