使用带有字符串连接的三元运算符

use*_*856 45 javascript

alert("test: "+(1==2)?'hello':'world');
Run Code Online (Sandbox Code Playgroud)

这应该'world'在屏幕上显示,因为1不等于2.

怎么警告'hello'

hun*_*ter 73

尝试围绕操作包裹你的parens

alert("test: "+ (1 == 2 ? 'hello' : 'world'));
Run Code Online (Sandbox Code Playgroud)

演示:http://jsfiddle.net/hunter/K3PKx/


这是做什么的:

alert("test: "+(1==2)?'hello':'world');
Run Code Online (Sandbox Code Playgroud)

正在评估"test: " + (1==2)作为true其输出'hello'


San*_*ord 17

两个提交的答案都是正确的,您需要添加括号.我想我会简要谈谈为什么.

alert("test: "+(1==2)?'hello':'world');
Run Code Online (Sandbox Code Playgroud)

当解析器遇到一个语句时,它将开始递归地将其分解为越来越小的块.

在这种情况下,它遇到的第一件事是一个函数:alert.解析器会立即查看alert参数,并开始分别解析每个参数.此函数只有一个参数,"test: "+(1==2)?'hello':'world'使其成为第一步.

在这个级别,我们可以将我们的陈述分解为一系列二进制比较.Javascript解析器从左到右形成二进制对(当操作值的顺序相同时).我们的潜在候选人"test: ",(1==2),'hello''world'与运营商+,?:.解析器将首先尝试添加"test: "(1==2).要做到这一点,它必须首先评估语句(1==2)(评估为false).该+操作会导致级联用绳子和强制所有原始变量试图代表自己的字串. false计算为"false"创建语句的字符串"test: false".

现在,解析器已准备好评估三元组的第一部分:"test: false"?.在Javascript中,所有非空字符串都会计算true,通过三元运算符的测试并选择第一个选项"hello".

通过在原始语句中添加一些额外的括号:

alert("test: " + ((1 == 2) ? 'hello' : 'world'));
Run Code Online (Sandbox Code Playgroud)

我们告诉解析器我们要评估三元运算符BEFORE连接.


Mik*_*uck 5

运算符优先级

所有运算符都有所谓的优先级。这就是语言确定操作顺序的方式。优先级较高的运算符将在优先级较低的运算符之前进行评估。操作的顺序允许以正确的顺序执行表达式。

例如,

1 + 2 * 3 == 1 + 6 == 7
Run Code Online (Sandbox Code Playgroud)

因为*它的优先级高于+。没有优先权,您将得到

1 + 2 * 3 == 3 * 3 == 9
Run Code Online (Sandbox Code Playgroud)

+?:

在JavaScript中,+运算符的优先级高于?:运算符。这意味着将在评估三元组中的条件之前进行连接。这可能会导致一些奇怪的结果。

注意:运算符的关联性和优先级可以在语言之间改变。例如,在JavaScript中,?:运算符是右关联的,但在PHP中左关联的。这些相同的比较将在这些语言之间产生不同的结果。

无论情况如何,都会发生相同类型的问题:

使用这些相同的优先级规则,我们知道括号(( ... ))具有其他所有运算符的最高优先级。通过将括号内的操作分组,将首先评估这些操作。这是递归的,允许您在更深的括号内进一步分组操作。

这样,您可以在括号内编写三元表达式以获得所需的结果。

var variable, str;

// Equality operators have higher precedence than ?: but lower than +
// so the below expression breaks down like this:
//   ("A" + variable) !== undefined ? "B" : ("C" + "D")
//   "Aundefined" !== undefined ? "B" : "CD"
//   true ? "B" : "CD"
//   "B"
str = "A" + variable !== undefined ? "B" : "C" + "D";
console.log(str);

// For the same reason as above, you get a strange result here.
// Here's how we break it down:
//   ("A" + variable) === undefined ? "B" : ("C" + "D")
//   "Aundefined" === undefined ? "B" : "CD"
//   false ? "B" : "CD"
//   "CD"
str = "A" + variable === undefined ? "B" : "C" + "D";
console.log(str);
Run Code Online (Sandbox Code Playgroud)

通常,将条件也括在括号中是一个好主意。这样,使用较低优先级的运算符生成条件时,您将获得正确的值。

// Check for undefined
var animal;
// Expected: "The animal does not exist", actual: undefined
console.log("The animal " + animal === undefined ? "does not exist" : animal);

// Expected: "The animal undefined", actual: "does not exist"
console.log("The animal " + animal !== undefined ? "does not exist" : animal);

// Check for null
animal = null;
// Expected: "The animal does not exist", actual: null
console.log("The animal " + animal === null ? "does not exist" : animal);

// Expected: "The animal null", actual: "does not exist"
console.log("The animal " + animal !== null ? "does not exist" : animal);

// Check for property
animal = {};
// Expected: "This animal doesn't have a type", actual: undefined
console.log("The animal " + animal.hasOwnProperty('type') ? animal.type : "doesn't have a type");

animal.type = 'is a dog';
// Expected: "This animal is a dog", actual: "is a dog"
console.log("The animal " + animal.hasOwnProperty('type') ? animal.type : "doesn't have a type");
Run Code Online (Sandbox Code Playgroud)