这是什么JS语法?表达式中的作业?(x!=空& amp; amp;(y = x)) [英] What is this JS syntax? Assignment in expression? (x != null && (y = x))

查看:76
本文介绍了这是什么JS语法?表达式中的作业?(x!=空& amp; amp;(y = x))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此JS插件,并且遇到了一些以前从未见过的语法.我了解它在做什么,但是我不确定它为什么起作用.

I'm working with this JS plugin, and I've encountered some syntax I've never seen before. I understand what it's doing, but I'm not sure why it works.

以下是其中一个实例的示例:

Here's an example of one instance of it:

settings.maxId != null && (params.max_id = settings.maxId);

这是否只是利用条件和单= =的优势?这是JS的通用语法吗?

Is this just taking advantage of conditionals and the single = ? Is this common syntax for JS?

推荐答案

在JavaScript中, = 运算符是表达式并评估分配的值.因为它是一个 expression ,所以它可以在允许表达的任何地方使用,即使它会产生副作用.因此:

In JavaScript the = operator is an expression and evaluates the assigned value. Because it is an expression it can be used anywhere an expression is allowed even though it causes a side-effect. Thus:

settings.maxId != null && (params.max_id = settings.maxId)

意思是:如果 settings.maxId 不为null,则(,然后只有,因为&&

Means: If settings.maxId is not null then (and only then, since && is short circuiting) evaluate the right-expression (params.max_id = settings.maxId) which in turn causes the value of settings.maxId to be assigned to params.max_id. This is much more clearly written as:

if (settings.maxId != null) {
  params.max_id = settings.maxId
}

快乐的编码.

这篇关于这是什么JS语法?表达式中的作业?(x!=空& amp; amp;(y = x))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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