无效的代码: console.clear();
try {
test(token);
}
catch(err) {
console.log(err.toString());
}
if(typeof token === undefined) {
console.log("token exists");
}
test(token);
if(token) {
console.log("token exists");
}
function test(o) {
if(o) {
console.log("token exists");
}
}
Run Code Online (Sandbox Code Playgroud)
JSBin: http ://jsbin.com/qanuk/1/edit?html,js,console,output
问题: 如果不存在参考错误,如何测试存在的"令牌"?
我的所有三个示例都抛出一个ReferenceError:未定义标记
使用:
if(typeof token === 'undefined')
Run Code Online (Sandbox Code Playgroud)
typeof 是关键字,而不是函数typeof 总是返回一个字符串| 归档时间: |
|
| 查看次数: |
643 次 |
| 最近记录: |