Ben*_*rce 0 javascript variables defensive-programming if-statement function
我试图使用javascript?:语法重写下面的语句.
if(type of someVariable !="undefined"){
someFunction(someVariable);
}else{}
Run Code Online (Sandbox Code Playgroud)
这是我目前的尝试,它导致语法错误
typeof someVariable != "undefined" ? someFunction(someVariable) : ;
Run Code Online (Sandbox Code Playgroud)
如果有人能说出我遇到了什么错,我会很感激.欢迎提供有关防御性编程最佳实践的任何附带提示.
?:style(需要表达式的两边:
):
typeof(someVariable) != 'undefined' ? someFunction : null;
Run Code Online (Sandbox Code Playgroud)
忍者风格:
someVariable !== undefined && someFunction(someVariable);
Run Code Online (Sandbox Code Playgroud)
[编辑:我无法宣誓noop
是Javascript中的一件事,但显然我错了.切换到null
]