本文介绍了这是什么JS语法?表达式中的作业?(x!=空&&(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,则(,然后只有,因为&& 短路)评估右表达式( params.max_id = settings.maxId ),进而导致将 settings.maxId 的值分配给 params.max_id .这是更清楚写为:

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!=空&&(y = x))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 13:25