JavaScript(x,x1,x2,…,xn)中逗号运算符的用途 [英] The purpose of the comma operator in JavaScript (x, x1, x2, …, xn)

查看:74
本文介绍了JavaScript(x,x1,x2,…,xn)中逗号运算符的用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript (x,x1,x2,…,xn)中始终返回 xn .

In JavaScript (x, x1, x2, …, xn) always returns xn.

在Python中,此代码称为元组,它返回不同的值.

In Python this code is called tuple and it returns different values.

我的问题是,这段代码在JavaScript中的用途是什么?你有一些实际的例子吗?

My question is what is the purpose of this code in JavaScript? Do you have some practical example?

推荐答案

逗号运算符对每个操作数求值,但仅返回最后求值的结果.可用于在for循环中初始化多个变量.

The comma operator evaluates every operand but only returns the result of the last evaluation. It can be used to initialize multiple variable in a for loop.

var str = "Hello, World!";
for(var i = 0, len = str.length; i < len; i++)
    console.log(str.charAt(i));

正如评论中指出的那样,这不是逗号运算符的真实示例,而只是 var .您可以查看此页面以获取实际示例.

As pointed out in the comments, this isn't a true example of the comma operator, this is just the var. You can see this page for actual examples.

这篇关于JavaScript(x,x1,x2,…,xn)中逗号运算符的用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆