简体   繁体   English

Javascript-类型检查的正确方法

[英]Javascript - proper way of type checking

I was suggested to change this: 建议我更改此设置:

if ( typeof user.account.products !== 'undefined') {}

to this: 对此:

if (user.account && user.account.hasOwnProperty('products'))

In second example user.account was added as an extra measure of defence, and it is valid. 在第二个示例中,添加了user.account作为额外的防御措施,并且它是有效的。 Now the other part got me curious. 现在另一部分让我感到好奇。

I understand what it does, but still cant wrap my head which one to use. 我了解它的作用,但仍然无法使用哪个头。

The first check means 第一次检查手段

if ( typeof user.account.products !== 'undefined') {
   // i will execute if products property is defined on user.account
   // even is null or 0
}

The second check means (simplified) 第二种检查方式(简体)

if (user.account.hasOwnProperty('products')) {
   // i will execute if user.account has a property of "products" even if products property is undefined/null/0 
   // and also i wont be executed if user.account is inherited from something has its own 
   // "products" property but not defined on user.account's prototype (inherited property).

}  

There is also a third option that you haven't mentioned is this 您还没有提到的第三种选择是

if ('products' in user.account) {
  // i will execute if user.account has a property of
  // "products" even if products property is undefined/null/0
}

When a property doesn't exist and you try to get its type or value, you get undefined . 当属性不存在并且尝试获取其类型或值时,将得到undefined If you convert undefined to a Boolean (which is what an if/then statement does with the expression supplied as the condition), you get false because certain values are truthy and others are falsey . 如果转换undefined为布尔(这是一个if/then语句,如提供的条件表达式一样),你会得到false ,因为某些价值观是truthy和其他人falsey

So this is an explicit way of testing for not undefined : 因此,这是测试undefined的显式方法:

if ( typeof user.account.products !== 'undefined') {}

And, this is an implicit way of doing the same thing: 而且,这是做相同事情的隐式方式:

if (user.account.products)

Now your line: 现在您的行:

if (user.account && user.account.hasOwnProperty('products'))

Is more specific than either of these because not only does it test to see if user.account exists, but it also tests to see if that object has its own products property. 比这两种方法中的任何一种都更具体,因为它不仅测试以查看user.account存在,而且还测试以查看该对象是否具有自己的products属性。 If it is that property you intend to use and there is a chance the user.account may not exist or that it may, but may not have products as a property, then this is the best way to test for it. 如果您打算使用该属性,并且user.account可能不存在,或者可能但不包含products作为属性,那么这是测试它的最佳方法。

But, there are still other ways to do this kind of testing. 但是,还有其他方法可以进行这种测试。 This: 这个:

if(account in user && products in user.account)

Checks to see if the respective properties exist in their host object without regard to whether the properties are inherited or not. 检查各自的属性是否存在于其宿主对象中,而不管这些属性是否被继承。

But, in the end, if it is the products property you care about, all you really need is: 但是,最后,如果这是您关心的products属性,那么您真正需要的就是:

if(products in user.account);

This test will fail if products isn't there or it user.account isn't valid. 如果products不存在或user.account无效,则此测试将失败。

It all depends on how granular you want to test. 这完全取决于您要测试的粒度。

If I get your question correctly, you are asking why using: 如果我正确回答了您的问题,那么您在问为什么使用:

user.account.hasOwnProperty('products')

instead of: 代替:

user.account.products !== undefined

In that case, the two options are valid. 在这种情况下,两个选项均有效。 With that saying, the new option (hasOwnProperty) is more elegant- Instead of assuming that the property exists, and then check if it is defined, you are asking if the object has this property. 这样说来,新选项(hasOwnProperty)更加优雅-您要询问对象是否具有此属性,而不是假设该属性存在,然后检查是否已定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM