Javascript 如何以三元形式编写此if-else块
如何将其写成三元形式:Javascript 如何以三元形式编写此if-else块,javascript,Javascript,如何将其写成三元形式: if (localStorage.getItem("txt")) { newNote(localStorage.getItem("txt"), localStorage.getItem("name")); } else { newNote(); } 这似乎不起作用: newNote(localStorage.getItem("txt") ? localStorage.getItem("txt"), localStorage.getItem("
if (localStorage.getItem("txt")) {
newNote(localStorage.getItem("txt"), localStorage.getItem("name"));
} else {
newNote();
}
这似乎不起作用:
newNote(localStorage.getItem("txt") ? localStorage.getItem("txt"), localStorage.getItem("name") ? newNote();
试试这个:
var note = localStorage.getItem("txt")?newNote(localStorage.getItem("txt"), localStorage.getItem("name")):newNote();
语法是boolean?expr1:expr2代码>
但我的建议是使用如下方式:
var note=getNode(localStorage.getItem("txt"),localStorage.getItem("name"));
...
function getNode(txt,name){
return txt.length>0?newNote(txt,name):newNode();
}
试试这个:
var note = localStorage.getItem("txt")?newNote(localStorage.getItem("txt"), localStorage.getItem("name")):newNote();
语法是boolean?expr1:expr2代码>
但我的建议是使用如下方式:
var note=getNode(localStorage.getItem("txt"),localStorage.getItem("name"));
...
function getNode(txt,name){
return txt.length>0?newNote(txt,name):newNode();
}
您不需要条件运算符。试试这个:
var txt = localStorage.getItem("txt");
newNote(txt, txt && localStorage.getItem("name"));
如果txt
是undefined
,那么newNote
将在没有任何参数的情况下被调用,如果是:使用txt
和name
您不需要条件运算符。试试这个:
var txt = localStorage.getItem("txt");
newNote(txt, txt && localStorage.getItem("name"));
如果txt
是undefined
,那么newNote
将在没有任何参数的情况下被调用,如果是:withtxt
和name
为什么这样做
如果设置了参数,只需在newNote
正文中进行检查
function newNote(a, b) {
a = a || 'default';
b = b || 'default';
}
你为什么要这样做
如果设置了参数,只需在newNote
正文中进行检查
function newNote(a, b) {
a = a || 'default';
b = b || 'default';
}
回答问题:
localStorage.getItem("txt")
? newNote(localStorage.getItem("txt"), localStorage.getItem("name"))
: newNote();
但老实说,我不明白为什么要这样做,它的可读性较差。我只想让newNote
函数的方式是,如果给定的参数是null
,它就像一个没有参数的newNote()
。在这种情况下,您可以拨打:
newNote(localStorage.getItem("txt"), localStorage.getItem("name"))
如果主代码中没有任何if
,则在newNote
功能中:
function newNote(text, name) {
if (text === null) {
// alias to `newNote()`
} else {
// do whatever with `text` and `name`
}
}
回答问题:
localStorage.getItem("txt")
? newNote(localStorage.getItem("txt"), localStorage.getItem("name"))
: newNote();
但老实说,我不明白为什么要这样做,它的可读性较差。我只想让newNote
函数的方式是,如果给定的参数是null
,它就像一个没有参数的newNote()
。在这种情况下,您可以拨打:
newNote(localStorage.getItem("txt"), localStorage.getItem("name"))
如果主代码中没有任何if
,则在newNote
功能中:
function newNote(text, name) {
if (text === null) {
// alias to `newNote()`
} else {
// do whatever with `text` and `name`
}
}
我喜欢@ZER0建议的方式,但我会这样做
为了便于阅读,我将localStorage.getItem(“txt”)
和localStorage.getItem(“name”)
分配给一个变量:
var a = localStorage.getItem("txt");
var b = localStorage.getItem("name");
test(a ? [a, b] : []);
function test(options){
if(options.length > 0){
alert("a");
}else{
alert("ab");
}
}
我喜欢@ZER0建议的方式,但我会这样做
为了便于阅读,我将localStorage.getItem(“txt”)
和localStorage.getItem(“name”)
分配给一个变量:
var a = localStorage.getItem("txt");
var b = localStorage.getItem("name");
test(a ? [a, b] : []);
function test(options){
if(options.length > 0){
alert("a");
}else{
alert("ab");
}
}
为什么?看看答案,它们远不如if
表单那么清晰。如果我必须用这里使用的三元形式来驱动一个项目,我可能会用第一种方式来编写它……所以我更喜欢If-else语句而不是三元语句。为什么?看看答案,它们远不如if
表单那么清晰。如果我必须用这里使用的三元形式来驱动一个项目,我可能会用第一种方式来编写它……因此我更喜欢If-else语句而不是三元语句。localStorage.getItem(“txt”)、localStorage.getItem(“name”)
应该是(localStorage.getItem(“txt”)
存在时的参数。是的,如果localStorage.getItem(“txt”)
返回truelocalStorage.getItem(“txt”),localStorage.getItem(“name”)应该是(localStorage.getItem(“txt”)
存在时的参数
iflocalStorage.getItem(“txt”)
返回true@Jonathan是的,没错true@Jonathan没错