Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么人们会这样写js?;_Javascript - Fatal编程技术网

Javascript 为什么人们会这样写js?;

Javascript 为什么人们会这样写js?;,javascript,Javascript,(0,\u reactIs.isMemo)真把我弄糊涂了。这是什么意思 ps:我知道(0,_reactIs.isMemo)这个表达式的值是_reactIs.isMemo那里的逗号运算符确保括号内的内容在没有调用上下文的情况下作为表达式进行计算 举一个简短的例子,如果代码是: var type = (0, _reactIs.isMemo)(nodeOrComponent) ? nodeOrComponent.type.type : nodeOrComponent.type; 然后将使用调用

(0,\u reactIs.isMemo)
真把我弄糊涂了。这是什么意思


ps:我知道
(0,_reactIs.isMemo)
这个表达式的值是
_reactIs.isMemo

那里的逗号运算符确保括号内的内容在没有调用上下文的情况下作为表达式进行计算

举一个简短的例子,如果代码是:

  var type = (0, _reactIs.isMemo)(nodeOrComponent) ? nodeOrComponent.type.type : nodeOrComponent.type; 
然后将使用调用上下文
obj
调用
fn
。但是原始的未编译代码,不管它是什么,都没有这样的调用上下文,因此为了忠实于原始代码,必须删除调用上下文,这可以通过逗号运算符完成:

var type = obj.fn(someArg); 
做同样事情的另一种方法是:

var type = (0, obj.fn)(someArg);
(但这需要更多字符,因此缩略语者更喜欢逗号运算符版本)


这是一个看起来很傻的缩小技巧,在导入的模块中经常可以看到。通常,您只会查看源代码,而不会有这种愚蠢。

也许这是某些“transpiler”(如babeljs)或“bundler”(如webpack)进程的结果-因此没有人实际编写过这种垃圾代码,但某些进程生成了这种有效但垃圾的代码
var fn = obj.fn;
var type = fn(someArg);