确定封装的javascript函数参数的值
好吧,首先让我们看一些代码:
<html>
<body>
<script type="text/javascript">
function logFunction(fn) {
console.log(fn.prototype);
console.log(fn);
console.log(fn(4));
}
var num = 5;
var add5 = function(x) { return x + 5 };
var addNum = function(x) { return x + num };
var adder = function(y) { return function(x) { return x + y } };
logFunction(add5);
logFunction(addNum);
logFunction(adder(5));
</script>
</body>
</html>
执行时,它返回以下结果:
Object
constructor: function (x) { return x + 5 }
__proto__: Object
function (x) { return x + 5 }
9
Object
constructor: function (x) { return x + num }
__proto__: Object
function (x) { return x + num }
9
Object
constructor: function (x) { return x + y }
__proto__: Object
function (x) { return x + y }
9
虽然在第一种情况下很容易看到 x 将添加到的值是 5,但我似乎无法想到以及对其他两种情况执行相同操作的方法。
我的问题是:有没有一种方法可以确定在最后两个示例中添加的 x 的值,仅使用函数引用和变量名称的知识(“num”,“y”等) .)?
编辑:
好吧,我可以看到找到这些值确实是不可能的。唯一的方法是我可以访问匿名函数的“arguments”属性,但可惜,这也是不可能的。我的解决方法是需要一个 Function 类型的对象作为参数。这仍然存在一些问题,如下面我的新代码所示,但这应该适用于我的情况。谢谢大家!
<html>
<body>
<script type="text/javascript">
function logFunction(fn) {
console.log(fn.prototype);
console.log(fn);
console.log(fn(4));
console.log("");
console.log("");
}
var num = 5;
var addNumFunc = new Function("x", "return x + " + num);
var whereNumFunc = new Function("x", "return x >= " + num * num);
var adderFunc = new Function("y", "return function(x) { return x + y }");
var adderFuncFunc = new Function("y", "return new Function(\"x\", \"return x + \" + y)");
logFunction(addNumFunc);
logFunction(whereNumFunc);
logFunction(adderFunc);
logFunction(adderFunc(5));
logFunction(adderFuncFunc);
logFunction(adderFuncFunc(5));
</script>
</body>
</html>
返回:
anonymous
function anonymous(x) {
return x + 5
}
9
anonymous
function anonymous(x) {
return x >= 25
}
false
anonymous
function anonymous(y) {
return function(x) { return x + y }
}
function (x) { return x + y }
Object
function (x) { return x + y }
9
anonymous
function anonymous(y) {
return new Function("x", "return x + " + y)
}
function anonymous(x) {
return x + 4
}
anonymous
function anonymous(x) {
return x + 5
}
Alright, to start with let's look at some code:
<html>
<body>
<script type="text/javascript">
function logFunction(fn) {
console.log(fn.prototype);
console.log(fn);
console.log(fn(4));
}
var num = 5;
var add5 = function(x) { return x + 5 };
var addNum = function(x) { return x + num };
var adder = function(y) { return function(x) { return x + y } };
logFunction(add5);
logFunction(addNum);
logFunction(adder(5));
</script>
</body>
</html>
When executed, it returns the following results:
Object
constructor: function (x) { return x + 5 }
__proto__: Object
function (x) { return x + 5 }
9
Object
constructor: function (x) { return x + num }
__proto__: Object
function (x) { return x + num }
9
Object
constructor: function (x) { return x + y }
__proto__: Object
function (x) { return x + y }
9
While it would be very easy in the first case to see that the value that x will be added to is 5, I cannot seem to come up with a way to do the same with the other two cases.
My question is this: Is there a way to determine the value of what x is being added to in the last two examples, armed only with the function reference and the knowledge of what the variable is called ("num", "y" etc.)?
EDIT:
Alright, I can see that finding these values is indeed impossible. The only way would be if I could have access to the anonymous function's 'arguments' property, but alas, that too is impossible. My work around for this is to require an Object of type Function as a parameter. There are still some problems with this, as can be seen in my new code below, but this should work for my case. Thanks everyone!
<html>
<body>
<script type="text/javascript">
function logFunction(fn) {
console.log(fn.prototype);
console.log(fn);
console.log(fn(4));
console.log("");
console.log("");
}
var num = 5;
var addNumFunc = new Function("x", "return x + " + num);
var whereNumFunc = new Function("x", "return x >= " + num * num);
var adderFunc = new Function("y", "return function(x) { return x + y }");
var adderFuncFunc = new Function("y", "return new Function(\"x\", \"return x + \" + y)");
logFunction(addNumFunc);
logFunction(whereNumFunc);
logFunction(adderFunc);
logFunction(adderFunc(5));
logFunction(adderFuncFunc);
logFunction(adderFuncFunc(5));
</script>
</body>
</html>
Which returns:
anonymous
function anonymous(x) {
return x + 5
}
9
anonymous
function anonymous(x) {
return x >= 25
}
false
anonymous
function anonymous(y) {
return function(x) { return x + y }
}
function (x) { return x + y }
Object
function (x) { return x + y }
9
anonymous
function anonymous(y) {
return new Function("x", "return x + " + y)
}
function anonymous(x) {
return x + 4
}
anonymous
function anonymous(x) {
return x + 5
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于 num:
num 是一个全局属性。如果您了解这一点(它是一个全局属性),那么您就知道可以像这样访问它:
对于 y:
y 是位于通过执行 adder 创建的函数的作用域链中的属性(5)。该函数可以访问 y 的值,但该函数外部的代码无法访问它。
函数closureFunction在其作用域链中有y属性,如果运行这个函数,y的值将被添加到传入的参数中。但是,如果重构代码,则可以检索 y 的值。
你的代码:
重构代码:
现在你可以像这样得到 y:
这应该被深思熟虑。
In the case of num:
num is a global property. If you have that knowledge (that it is a global property), then you know that you can access it like so:
In the case of y:
y is a property that is in the scope chain of the function that was created by executing adder(5). That function has access to the value of y, but the code outside of that function does not have access to it.
The function closureFunction has the y property in its scope chain, and if you run this function, the value of y will be added to the passed-in argument. However, if you refactor your code you could enable the retrieval of the value of y.
Your code:
Refactored code:
Now you can get y like so:
This should be thought trough.