简体   繁体   English

关于自执行功能的奇怪之处

[英]Weird thing about Self-executing functions

I stumbled upon something in a validation library where they use a required-validation as part of a validation function. 我在验证库中偶然发现了一些东西,他们使用必需验证作为验证功能的一部分。

Now, the weirdness that I cant seem to find or read myself to sanity about has to do with self-executing functions. 现在,我似乎无法找到或阅读自己的理智与自我执行功能有关。 This is from the code. 这是来自代码。

value => !(0,req)(value) || [...some more checks]

Now.. the req() method takes one argument, so far we're good. 现在.. req()方法接受一个参数,到目前为止我们都很好。 What got me interested was that we could write req(value) or (req)(value) just as well.. but that little zero in front seems like totally useless. 让我感兴趣的是我们可以写req(value)(req)(value) ......但是前面的那个小零似乎完全没用。

I tried a lot of stuff here, using alert() and came to one weird conclusion. 我在这里尝试了很多东西,使用alert()并得出了一个奇怪的结论。 I ran these cases inspector console for testing the output. 我运行这些案例检查器控制台来测试输出。 works just as fine as which works just as fine as the following: 工作得和以下一样好:

(alert)('hello') » gives a 'hello' alert (alert)('hello') »发出'你好'警告

(0,alert)('hello') » gives a 'hello' alert (0,alert)('hello') »发出'你好'警告

(0,1,3,5,1,alert)('hello') » gives a 'hello' alert (0,1,3,5,1,alert)('hello') »发出'你好'警告

(console.log,alert)('hello') » gives a 'hello' alert (console.log,alert)('hello') »发出'hello'警告

(alert,1)('hello') » gives an error (alert,1) is not a function (alert,1)('hello') »给出错误(alert,1) is not a function

(alert,console.log)('hello') » gives a console log 'hello (alert,console.log)('hello') »给出一个控制台日志'hello

(alert,1,"test",console.log)('hello') » gives a console log 'hello (alert,1,"test",console.log)('hello') »给出一个控制台日志'hello


Can anybody explain to me how this resolution works? 任何人都可以向我解释这个决议是如何运作的吗?

Why does it seem to go with the latest method provided and therefore (0,alert) do the same as (alert) and as (0, console.log, "test", alert) ? 为什么它似乎与提供的最新方法一起使用,因此(0,alert)(alert)(0, console.log, "test", alert)

It because It take last value from the brackets and execute the code: 这是因为它从括号中取出最后一个值并执行代码:

  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 

There isn't anything specific about methods/functions here. 这里没有关于方法/功能的具体内容。 You're just seeing the Comma operator in action. 你刚刚看到Comma运营商在行动。

The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand. 逗号运算符计算其每个操作数(从左到右)并返回最后一个操作数的值。

For example, (alert,1,"test",console.log) evaluates to console.log , and then the ('hello') afterward is just doing a call on that function. 例如, (alert,1,"test",console.log)计算到console.log ,然后('hello')只是对该函数进行调用。

It's confusing because the parentheses in this code are used for two separate things: surrounding an expression or calling a function. 这很令人困惑,因为此代码中的括号用于两个单独的事情:围绕表达式或调用函数。 It only means "calling a function" if it follows a function. 如果它跟随一个函数,它只意味着“调用一个函数”。 Otherwise, the comma operator is what will kick in. 否则,逗号运算符将启动。

(note that minifiers are able to use the comma operator to shrink down code, but please don't use in real code since it's super esoteric). (请注意,缩小器能够使用逗号运算符缩小代码,但请不要在实际代码中使用,因为它是超级深奥的)。

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

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