想要检查空值。 any()
方法返回null
或匹配结果数组(实际上有一个match()
方法返回)。在Javascript中检查空值(使用三元运算符)
$scope.isMobileBrowser = !isMobile.any() ? false : true;
如果any()
方法返回null我想false
被分配到$scope.isMobileBrowser
变量,否则true
。在任何可能的情况下,上述片段是否会失败?有没有其他更有效的解决方法?
为isMobile
对象的更多细节:
var isMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i);
},
any: function() {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};
*是否有其他更有效的解决方法?* - 您确定您的程序由于此行而变得缓慢吗? – thefourtheye
不,它不慢。我想知道正确性,如果还有其他更好的选择。 – fatCop
如果您确实不需要返回的数组,请使用'test()'而不是'match()'。 – Teemu