我有一个组件可以user从它的authprop中解构:
const Profile = ({
auth: {user}
}) => {...}
Run Code Online (Sandbox Code Playgroud)
问题是当我在开发时,Nodemon 会在我保存任何更改时不断刷新我的页面。当组件尝试挂载时,它会抛出一个错误,它无法user从中解构,auth因为此时auth为空(直到我导航该站点并重新登录)。
有没有一种优雅的方法来处理这个问题?我看了这篇文章,但我不能做类似的事情const { user } = auth || {}。嗯.. 我的意思是,我可以,但我想从 props 中解构,而不是const { user } = auth || {}在函数体中。
我希望保护我的函数不受null-ish值的影响,只有在存在"已定义"值时才会继续.
之后找 周围的解决方案,建议增加一倍等于未定义:if (something == undefined).此解决方案的问题是您可以声明未定义的变量.
所以我目前的解决方案是检查null if(something == null),这意味着检查undefined.如果我想捕捉附加值假值,我检查if(something).
请参阅此处的测试:http://jsfiddle.net/AV47T/2/
我现在错过了什么吗?
马蒂亚斯
从它提到的可维护的JavaScript这本书中提到:
// Bad: Testing to see if an argument was passed
function doSomething(arg1, arg2, arg3, arg4){
if (arg4 != null){
doSomethingElse();
}
}
Run Code Online (Sandbox Code Playgroud)
但我觉得!= null在这里使用是非常有效的,它过滤了未传递的参数或传递为null的情况
为什么作者认为它是坏的?
Javascript采用了一种语法,您可以在其中执行逻辑检查,而无需检查任何内容:
if (foo) {
}
Run Code Online (Sandbox Code Playgroud)
这相当于什么?是吗:
if (foo != null) { }
if (foo !== null) { }
if (typeof(foo) != 'undefined') { }
if (typeof(foo) !== 'undefined') { }
if (typeof(foo) != 'object') { }
if (typeof(foo) !== 'Object') { }
Run Code Online (Sandbox Code Playgroud)
我要求的实际动机是要确保成员"存在"(也就是说,如果它存在null或者undefined它不存在):
if (window.devicePixelRatio !== null)
if (window.devicePixelRatio != null)
if (!(window.devicePixelRatio == null))
if (!(window.devicePixelRatio === null))
if (!(window.devicePixelRatio == undefined))
if (!(window.devicePixelRatio === undefined))
if ((window.devicePixelRatio !== undefined))
Run Code Online (Sandbox Code Playgroud)
我担心的是,如果成员被定义,但被定义为 …
关于未定义变量,我在条件方面遇到了一些问题.总结一下,检查变量是否未定义的最佳方法是什么?
我主要是在苦苦挣扎
x === undefined
Run Code Online (Sandbox Code Playgroud)
和
typeof x === 'undefined'
Run Code Online (Sandbox Code Playgroud) 我有三个页面使用相同的代码,在其中一个页面上此变量不存在,另外两个变量ticketType的值为1或2.我需要先检查ticketType是否存在且未定义其次,需要确定它是1还是2.
此if语句生成错误:
if(typeof ticketType != undefined && ticketType == 1){}
Run Code Online (Sandbox Code Playgroud)
这是说ticketType isn't defined.我尝试嵌套if语句来检查它是否首先被定义为认为它不会去尝试内部if语句但firebug仍然会生成错误.
有任何想法吗?必须有办法做到这一点......
我一直以为我可以通过比较未定义的var来检查未定义的var,但这是我在chrome控制台中得到的错误:

我如何检查jQuery未定义的对象?
编辑:

if(jQuery)也给我带来了问题
编辑:
方案:
if(window.jQuery)作品.
typeof(jQuery) == 'undefined'也有效.
谁有人解释为什么?
我的问题是关于更有效地防止“变量未定义”错误。
例如
如果我使用以下代码:
if (array[index] != undefined)
{
if (array[index].id == 1)
{
// code
}
}
Run Code Online (Sandbox Code Playgroud)
它会正常工作。但是,我可以使用
if (array[index] != undefined && array[index].id == 1)
{
// code
}
Run Code Online (Sandbox Code Playgroud)
没有收到“变量未定义”错误,如果array未定义?
(我现在不能在我的代码中完全测试这个,因为我正在构建一个客户端 - 服务器应用程序,我必须改变很多行才能尝试它,所以我在这里问。对不起,如果不合适)
我想在我的网站上实现一个搜索功能,所以我做的是用快速服务器的文本进行jquery ajax调用,搜索mongodb并给出匹配的用户的对象数组.现在我成功地收到了这个对象,但由于ejs上没有部分内容,我如何只刷新为每个用户生成html的结果列表?
我们使用的jQuery版本是1.10.2.
在特定的屏幕上,我们使用jQuery获取DOM元素并尝试获取其offset()对象.有时可能不会显示该对象,并且根据jQuery文档,在这种情况下,不支持offset().
这一切都很好,但是,如果我尝试检查offset()对象是否与undefined不同,代码总是在条件内部,那么为什么我的头部旋转真的是为什么?
我的代码(由于保密协议而简化)如下:
var isCurrent = $('.detail-current[value=\'true\']:first');
if (isCurrent != 'undefined') {
var button = isCurrent.closest('.batch-item').find('.batch-item-top').find('input[name=\'edit\']');
var myOffsetTop = 0;
if(button.offset() != 'undefined')
myOffsetTop = button.offset().top;
$('html, body').animate({
scrollTop: myOffsetTop - 50
});
}
Run Code Online (Sandbox Code Playgroud)
当我使用IE开发工具调试时,我在控制台上看到button.offset()未定义但是button.offset()!= undefined返回true !!
有谁知道为什么会这样,以及我们如何才能最好地处理它?
谢谢.