如何确定变量undefined
还是null
?我的代码如下:
var EmpName = $("div#esd-names div#name").attr('class');
if(EmpName == 'undefined'){
//DO SOMETHING
};
Run Code Online (Sandbox Code Playgroud)
<div id="esd-names">
<div id="name"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
但是如果我这样做,JavaScript解释器会暂停执行.
我有一个场景,我将数据从reducer传递到我的反应状态.
数据:
{
"id": 1,
"title": "Test",
"content": {
"body": "sdfsdf"
"image": "http://example.com"
}
}
Run Code Online (Sandbox Code Playgroud)
使用componentWillRecieveProps,这非常适合检索标题.
componentWillReceiveProps(nextProps) {
this.setState({
title: nextProps.blog.title,
})
}
Run Code Online (Sandbox Code Playgroud)
但是,我在检索嵌套字段时遇到了困难.当我这样做:
componentWillReceiveProps(nextProps) {
console.log("new title is", nextProps.blog.title);
console.log("new body content is", nextProps.blog.content["body"]);
this.setState({
title: nextProps.blog.title,
body: nextProps.blog.content["body"]
})
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
单击调试器并加载内容后,未定义主体的错误消失.无论如何我能解决这个问题吗?
我试着像这样检查undefined:
if (typeof nextProps.blog.content["body"] != 'undefined'){
Run Code Online (Sandbox Code Playgroud)
但这也不起作用,我相信这是因为博客未定义.