Javascript Find参数传递给函数 [英] Javascript Find argument passed to a function

查看:35
本文介绍了Javascript Find参数传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到从函数传递给函数的参数.

I am needing to find the argument passed to a function from the function.

假设我有一个名为 foo 的函数:

Let us suppose I have a function called foo:

function foo() {
  var a = 3;
  var b = "hello";
  var c = [0,4];
  bar(a - b / c);
  bar(c * a + b);
}

function bar(arg) { alert(arg) }

当然,就像现在一样,bar 会一直提醒 NaN.

As it is now, of course, bar will always alert NaN.

在函数 bar 内部,我想以最初传递的形式获取参数.此外,我希望能够从 bar 函数访问 abc 的值.换句话说,我想要这种性质的东西:

Inside of the function bar, I want to obtain the argument in the form it was originally passed. Furthermore, I want to be able to access the values of a, b, and c from the bar function. In other words, I would like something of this nature:

bar(a - b / c);    

function bar() {
 //some magic code here
 alert(originalArg); //will alert "a - b / c"
 alert(vars.a + " " + vars.b + " " + vars.c); //will alert "3 hello 0,4"
} 

您可能认为这是不可能的,但我知道您可以使用 Javascript 做一些奇怪的事情.例如,您可以像这样显示调用函数的代码:

You may not think this is possible, but I know you can do weird things with Javascript. For example, you can display the code of the calling function like this:

function bar() {
  alert(bar.caller);
}

我愿意赌几美元,通过某种方式,您可以获得参数的原始形式和参数中变量的值.

I am willing to bet a few dollars that with some sort of finagling you can get a the original form of the argument and the values of the variables in the argument.

如果允许您以这样的形式调用 bar,我可以很容易地看到您将如何做到这一点:

I can easily see how you could do it if you were allowed to call bar in a form like this:

bar(function(){return a - b / c}, {a: a, b: b, c: c});

但这太复杂了,因此无法接受.bar 的调用方式可能不会改变.

But that is too convoluted and therefore unacceptable. The way bar is called may not be changed.

推荐答案

你的问题被误导了,而且很恶心......我喜欢它!这是我将解释的简短解决方案.

Your question is misguided and sick and...I love it! Here is a short solution which I will explain.

function foo() {
    var a = 3, b = "hello", c = [0, 4];
    bar(a - b / c); 
}

function bar(n) {
    alert([n, a, b, c, eval(n)].join('   '));
}

function meld(f, g) {
    f = f.toString(); g = g.toString();
    f = f.replace(/function\s+\w+/, 'function ');

    var n = g.match(/function\s+(\w+)/)[1];
    f = f.replace(new RegExp(n + '\\((.*?)\\)', 'g'), n + '("$1")');

    var i = f.indexOf('{') + 1;
    f = f.slice(0, i) + g + f.slice(i);

    eval('var ret = ' + f);
    return ret;
}

foo = meld(foo, bar);
foo();

关键是 meld(f, g) 函数,它返回一个新的匿名函数,该函数就像 f 一样,同时调用并将其内部结构暴露给 f 的副本代码>g.你看,原来的 g 在看到任何关于 f 的东西时处于不利的位置——充其量它可以使用 g.caller 和大量的正则表达式.

The key is the meld(f, g) function which returns a new anonymous function that acts just like f while calling and exposing its internals to a copy of g. You see, the original g is in a bad position to see anything about f -- at best it can use g.caller and tons of regular expressions.

这是meld 的伪代码.首先使 f 成为一个匿名函数,适合以后对变量求值和赋值;例如 function foo() { 变成 function() {.接下来找到 g 的真实姓名并引用从 new f 传递给它的任何参数.接下来,在新的 f 中插入 g 的副本.它将屏蔽全局名称,并且可以访问 f 的局部变量,就像它们是它自己的一样.最后,评估这个匿名函数并返回它.

Here is the pseudo code for meld. First make f an anonymous function, suitable for evaluating and assigning to a variable later on; for instance function foo() { becomes function() {. Next find g's real name and quote any arguments passed to it from new f. Next, insert a copy of g inside new f. It will mask the global name and it will have access to f's local variables as if they were its own. Finally, evaluate this anonymous function and return it.

那么我们如何使用meld?只需将 foo 替换为 meld(foo, bar),然后像往常一样使用 foo.

So how do we use meld? Just replace foo with meld(foo, bar) and then use foo as you normally would.

好的,现在是限制.我不想花费大量精力来改进 meld 中的正则表达式,以引用 g 的参数反对所有可能性.这只会分散对整个解决方案概念的注意力.您需要更改它以正确处理多个参数并转义任何本身带有引号或括号的参数.

OK, now for the limitations. I didn't want to spend lots of effort refining the regex inside meld to quote g's argument(s) against all possibilities. This would just have been a distraction from the concept of the solution as a whole. You'll need to change it to properly handle more than one argument and escape any which themselves have quotes or parentheses.

这篇关于Javascript Find参数传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆