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

标签 javascript

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

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/

相关文章:

javascript - 在 Javascript + MVC3 中使用 Json 解析实体列表

javascript - 如何使用 Javascript 获取 web url 内容

java - 根据页面 URL 替换页面上的文本

javascript - 如何使用 `String.substring` 将文本字符串的开头设置为 JavaScript 数组中 `<span>` 之前的 5 个字符?

javascript - 如何在 node.js 中获取字符串的 sha1 哈希?

javascript - Firebase 和 Algolia 确保搜索没有搜索命中

javascript - JavaScript 中的结构和面向对象

javascript - 如何从 jquery .val() 中查找某些字符串

javascript - Flow - 子类型数组

Javascript keydown 事件仅在最多两个键时触发