Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何以三元形式编写此if-else块_Javascript - Fatal编程技术网

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
将在没有任何参数的情况下被调用,如果是:with
txt
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”)
返回true
localStorage.getItem(“txt”),localStorage.getItem(“name”)应该是
(localStorage.getItem(“txt”)
存在时的参数
if
localStorage.getItem(“txt”)
返回true@Jonathan是的,没错true@Jonathan没错