我在验证库中偶然发现了一些东西,他们在其中使用了required-validation作为验证功能的一部分。

现在,我似乎找不到或无法理解的怪异与自我执行功能有关。这是从代码。
value => !(0,req)(value) || [...some more checks]
现在.. req()方法采用一个参数,到目前为止我们还不错。让我感兴趣的是,我们也可以编写req(value)(req)(value)。.但是前面的小零似乎完全没用。

我在这里使用alert()尝试了很多东西,得出了一个奇怪的结论。我运行了这些案例检查器控制台以测试输出。
的工作原理与以下相同:
(alert)('hello')»发出“你好”警报
(0,alert)('hello')»发出“你好”警报
(0,1,3,5,1,alert)('hello')»发出“你好”警报
(console.log,alert)('hello')»发出“你好”警报
(alert,1)('hello')»给出错误的(alert,1) is not a function(alert,console.log)('hello')»提供控制台日志“你好
(alert,1,"test",console.log)('hello')»提供控制台日志“你好

谁能向我解释该决议的工作原理?

为什么似乎要使用提供的最新方法,因此(0,alert)(alert)(0, console.log, "test", alert)一样?

最佳答案

这是因为它从方括号中获取最后一个值并执行代码:

    a = [1, 2, 3]
    b = [4, 5, 6]
    console.log((a,b)[0]) //prints 4
    //because Its JavaScript's default behaviour
    x = function(){alert("foo")};
    y = function(){alert("bar")};
    (x,y)() // alerts bar
    //because it takes last function which is y
    //also  for simple variable
    p = 10;
    q = 20;
    console.log((p,q)); //prints 20

10-07 18:56
查看更多