Check if Javascript boolean is set?

This question already has an answer here:

  • How do I check if an object has a property in JavaScript? 22 answers

  • 如果没有设置值,那意味着它是undefined

    function printStuff(params) {
      if (params.hello !== undefined) {
        console.log(params.hello);
      } else {
        console.log('Hello, ');
      }
    }
    
    printStuff({ });
    printStuff({
      hello: 'World'
    });

    If you want to check whether an object has a specific property, that's the in keyword:

    'required' in params
    

    There's also the hasOwnProperty method, if you need to exclude properties inherited from a prototype (you probably don't for this case):

    params.hasOwnProperty('required')
    

    这会解决你的问题吗?

    if(required!=undefined)
    
    链接地址: http://www.djcxy.com/p/27322.html

    上一篇: 在Object中查找属性

    下一篇: 检查是否设置了Javascript布尔值?