简体   繁体   English

在javascript中,评估的参数顺序和延迟评估

[英]in javascript, argument order of evaluation and delayed evaluation

  1. if function( arg1, arg2 ) , am I correct in believing arg1 is guaranteed to evaluated before arg2 (unlike classic C for example)? 如果function( arg1, arg2 ) ,我相信arg1是否正确保证在arg2之前进行评估(例如,与经典C不同)?

  2. is there any way to have a function where the arguments are not evaluated, but rather evaluated on demand? 有没有办法让一个函数不对参数进行评估,而是根据需要进行评估? for example, if( cond1 || cond2 ) evaluates cond2 if and only if cond1 is false. 例如, if( cond1 || cond2 )当且仅当cond1为false时才计算cond2 is it possible to write our own if -like function? 是否有可能编写我们自己的if like函数?

for example, could I write a function like oracle's nvl( arg1, arg2, ... ) , which returns the first non-null argument, evaluating them lazily. 例如,我可以写一个像oracle的nvl( arg1, arg2, ... )这样的函数,它返回第一个非null参数,懒惰地评估它们。 in a regular function call, all arguments are evaluated before the function body is executed. 在常规函数调用中,在执行函数体之前评估所有参数。

Function arguments are evaluated before they're passed to the function, so what you're asking for isn't technically possible. 函数参数在传递给函数之前会被评估,因此您所要求的在技术上是不可能的。 However, you could do this: 但是,你可以这样做:

function nvl() {
    for (var i = 0; i < arguments.length; i++) {
        var res = arguments[i]();
        if (res)
            return res;
    }
}

nvl(function() { return false; }, 
    function() { return 7; }, 
    function() { return true; });

The call to nvl returns 7 . nvl的调用返回7

All of the function wrappers are created, but only the bodies of the first two are evaluated inside nvl . 创建了所有函数包装器,但只在nvl中计算了前两个的主体。

This isn't exactly pretty, but it would allow you to prevent an expensive operation in the body of any of the functions after the first one returning a truthy value. 这不是很漂亮,但是它可以让你在第一个返回真值之后阻止任何函数体内的昂贵操作。

in your first example, yes, if you're calling a function they will be evaluated in order. 在你的第一个例子中,是的,如果你正在调用一个函数,他们将按顺序进行评估。

In the 2nd example, JS does not evaluate every param (just like C), eg: 在第二个例子中,JS不评估每个参数(就像C一样),例如:

if(true || x++){

x++ will never run. x ++永远不会运行。

What exactly are you trying to do? 你究竟想做什么?

  1. Yes
  2. Yes, with if(cond1 || cond2) , cond2 will be evaluated iff cond1 is false. 是的,使用if(cond1 || cond2) ,如果cond1为false,将评估cond2。

Here is a function that return the first truthy argument: 这是一个返回第一个truthy参数的函数:

function firstDefinedArgument(){
    for(var i in arguments){
        if(arguments[i]){
           return arguments[i];
        }
     }
 }

So, for example, firstDefinedArgument(0, null, undefined, "", "hello", "goodbye") would return "hello" . 因此,例如, firstDefinedArgument(0, null, undefined, "", "hello", "goodbye")将返回"hello"

If you wanted to return the first non-null argument instead of the first truthy one, you would replace if(arguments[i]) with if(arguments[i] !== null) . 如果你想返回第一个非空参数而不是第一个非空参数,你可以用if(arguments[i] !== null)替换if(arguments[i]) if(arguments[i] !== null) This function would return 0 in the above example. 在上面的例子中,该函数将返回0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM