请问return 后的括号两个进行与比较是什么意思
function renderItem(params, api) {
var rectShape ={
x:1,
y:2,
}
return (
rectShape && {
type: 'rect',
transition: ['shape'],
shape: rectShape,
}
);
}
请问return 后的括号两个进行与比较是什么意思
function renderItem(params, api) {
var rectShape ={
x:1,
y:2,
}
return (
rectShape && {
type: 'rect',
transition: ['shape'],
shape: rectShape,
}
);
}
这是js里一种比较常见的写法,因为js的逻辑运算其实是有返回值的。
rectShape && {
type: 'rect',
transition: ['shape'],
shape: rectShape,
}
这里的结果就是如果rectShape 不存在就返回false,如果rectShape 存在就返回后面的那个对象。
一般用来做判断空值简便写法,比如后面那个对象包含对rectShape的引用,那么如果要求rectShape不能为空可以这么写。
但是这段代码里面rectShape肯定不是空不知道为什么要这么写