js中的条件运算符

时间:2016-06-01 08:10:56

标签: javascript comparison

我知道带问号的条件运算符的用法。

$('body').sortable(
{
  items: 'div[data-type="global_container"]'
});

$('.global_container').sortable(
{
    connectWith: 'div[data-type="global_container"]',
  items: 'div[data-type="zone"]'
});

$('.global_container_2col').sortable(
{
    connectWith: '.global_container_2col, .global_container',
  items: '.zona_izquierda'
});

$('.global_container_1col').sortable(
{
    connectWith: '.global_container_1col, .global_container',
  items: '.zona_derecha'
});

我问的是这个; 是否可以在条件运算符中分配多个值?像这样;

var number = 5;
return number === 5 ? true : false;

2 个答案:

答案 0 :(得分:0)

类似的东西,

var [age, grade] = (number === 5) ? [21, 2] : [22, 3]

示例

> number = 2
> var [age, grade ] = (number === 5) ? [21, 2] : [ 22, 3 ]
> age
< 22
> grade
< 3

答案 1 :(得分:0)

这是有效的javascript,你有什么

var number = 5, age, grade;

number === 5 ? (age = 21, grade = 2) : (age = 22, grade = 3);

console.log(age);
console.log(grade);