gpt4 book ai didi

javascript - 当它是函数中的参数时,什么时候真正发生操作

转载 作者:行者123 更新时间:2023-11-28 16:05:27 26 4
gpt4 key购买 nike

假设我有一个如下所示的函数:

var meowLikeACat = function(howoften) { 
setInterval(function() {
alert("meow");
},howoften*1000)
}

假设我这样调用它:

var howOften_local = 3;
meowLikeACat(howOften_local+1);

+1 操作实际上在什么时候发生?

是否可以“拦截”函数运行之前传递的数据?

最佳答案

At what point does that +1 operation actually occur?

所有参数在传递给函数之前都会从左到右进行评估。

11.2.3 Function Calls

The production CallExpression : MemberExpression *Arguments* is evaluated as follows:

  1. Let ref be the result of evaluating MemberExpression.
  2. Let func be GetValue(ref).
  3. Let argList be the result of evaluating Arguments, producing an internal list of argument values (see 11.2.4).
  4. If Type(func) is not Object, throw a TypeError exception.
  5. If IsCallable(func) is false, throw a TypeError exception.
  6. If Type(ref) is Reference, then
    a. If IsPropertyReference(ref) is true, then
        i. Let thisValue be GetBase(ref).
    b. Else, the base of ref is an Environment Record
        i. Let thisValue be the result of calling the ImplicitThisValue concrete method of GetBase(ref).
  7. Else, Type(ref) is not Reference.
    a. Let thisValue be undefined.
  8. Return the result of calling the [[Call]] internal method on func, providing thisValue as the this value and providing the list argList as the argument values.

11.2.4 Argument Lists

The evaluation of an argument list produces a List of values (see 8.8).

The production Arguments : ( ) is evaluated as follows:

  1. Return an empty List.

The production Arguments : ( ArgumentList ) is evaluated as follows:

  1. Return the result of evaluating ArgumentList.

The production ArgumentList : AssignmentExpression is evaluated as follows:

  1. Let ref be the result of evaluating AssignmentExpression.
  2. Let arg be GetValue(ref)
  3. Return a List whose sole item is arg.

The production ArgumentList : ArgumentList , AssignmentExpression is evaluated as follows:

  1. Let precedingArgs be the result of evaluating ArgumentList.
  2. Let ref be the result of evaluating AssignmentExpression.
  3. Let arg be GetValue(ref).
  4. Return a List whose length is one greater than the length of precedingArgs and whose items are the items of precedingArgs, in order, followed at the end by arg which is the last item of the new list.
<小时/>

And would there be any way of forcing the operation to occur in any of the above?

前两个是相同的。我不明白你如何区分调用函数的时间和将参数传递给函数的时间。

您可以通过传递函数来完成第三个:

function meowLikeACat(howoften) {
setInterval(function() {
alert("meow");
}, howoften()*1000)
}

var howOften_local = 3;
meowLikeACat(function () {
return howOften_local+1;
});
<小时/>

And would it be possible to 'intercept' the data that was being passed before the function ran?

如果你的代码假设合作而不是敌意,这很简单:

function meowLikeACat(howoften) { 
setInterval(function() {
alert("meow");
}, howoften*1000)
}

function intercept(thisArg, original, before) {
return function() {
// could manipulate arguments here,
// or pass something completely different to original
before.apply(thisArg, arguments);
original.apply(thisArg, arguments);
};
}

function doBefore() {
console.log('before', arguments);
}

var meowLikeACatIntercepted = intercept(null, meowLikeACat, doBefore);

演示:http://jsfiddle.net/mattball/e4AY6

关于javascript - 当它是函数中的参数时,什么时候真正发生操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15215487/

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