xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js comma operator All In One

js comma operator All In One

The comma operator (,) evaluates each of its operands (from left to right) and returns the value of the last operand.
This lets you create a compound expression in which multiple expressions are evaluated, with the compound expression's final value being the value of the rightmost of its member expressions.
This is commonly used to provide multiple parameters to a for loop.

js 逗号运算符 (,) 计算其每个操作数(从左到右)并返回最后一个操作数
这使您可以创建一个复合表达式,在其中计算多个表达式,复合表达式的最终值是其最右边的成员表达式
这通常用于为 for 循环提供多个参数。

let x = 1;

x = (2, 3, 7);

console.log(x);
// 7

image

typeof (0, console.log)(`abc`)
// abc
// 'undefined'

typeof (0, console.log)
// 'function'

image

log = (0, 1, 2, 3, console.log);
// ƒ log() { [native code] }
log(`test`);
//  test
// undefined

log = (0, console.log);
// ƒ log() { [native code] }
log(`test`);
//  test

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator

demo

(0, console.log)("hello world!")
// hello world!

function test(a, b) {
   console.log(`a =`, a);
   console.log(`b =`, b);
}

(test)(111, 222)
// a = 111
// b = 222

(0, test)(111, 222)
// a = 111
// b = 222

(1, test)(111, 222)
// a = 111
// b = 222

https://stackoverflow.com/questions/62171790/whats-going-on-behind-this-js-function-call-trick-0-console-loghello-worl

https://stackoverflow.com/questions/32275135/why-does-babel-rewrite-imported-function-call-to-0-fn

refs



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-02-28 23:58  xgqfrms  阅读(36)  评论(2编辑  收藏  举报