简体   繁体   English

带有动态生成属性的 JavaScript object.hasOwnProperty()

[英]JavaScript object.hasOwnProperty() with a dynamically generated property

I have an object that I am passing to a function, that I am trying to figure out if the property exists or not, and when it doesn't, ignore it.我有一个要传递给函数的对象,我试图确定该属性是否存在,如果不存在,则忽略它。

The problem is I keep getting false even when the property is there.问题是即使房产在那里,我也一直在false For sake of example, I will use an object I posted on another question earlier today...例如,我将使用我今天早些时候在另一个问题上发布的对象...

var myObj = {
       something1_max: 50,
       something1_enabled: false,
       something1_locked: true,
       something2_max: 100,
       something2_enabled: false,
       something2_locked: true,
       something3_max: 10,
       something3_enabled: true,
       something3_locked: true
    }

which gets passed to a function like: buildRetentionPolicyStr('something2', myObj);它被传递给一个函数,如: buildRetentionPolicyStr('something2', myObj);

So far I've got everything I need with this function working perfectly.到目前为止,我已经有了这个功能完美运行所需的一切。 Until I tried it on live data and realized on the occasion, properties I thought were static and there with defaults otherwise aren't always actually there.直到我在实时数据上尝试它并在这种情况下意识到之前,我认为属性是静态的并且具有默认值,否则并不总是实际存在。 So I need to do something I assume with hasOwnProperty() somehow.所以我需要以某种方式用hasOwnProperty()做一些我假设的事情。 So in my function I can set a default of my own where if the property exists, use it..因此,在我的函数中,我可以设置自己的默认值,如果该属性存在,请使用它。

Ie: IE:

function buildRetentionPolicyStr(theScope, obj)
{
   var myVar = 0;
   if(obj.hasOwnProperty(theScope + '_enabled'))
   {
       myVar = obj[theScope + '_enabled'];
   }
}

In my current test case, the object does in fact exist, so I know that to be true.在我当前的测试用例中,对象确实存在,所以我知道这是真的。 However, when I do (right above the if statement):但是,当我这样做时(就在if语句上方):

console.log(obj.hasOwnProperty(theScope + '_enabled'));
// Or
console.log(obj.hasOwnProperty([theScope + '_enabled']));

I get this output respective to the order above:我得到了与上述顺序相关的输出:

false
// Or
["something2_enabled"]

What is, if there is one, the proper way to check to see if the property exists in this fashion?什么是,如果有的话,检查财产是否以这种方式存在的正确方法是什么?

A simple way to do that is to run typeof against your property:一种简单的方法是对您的财产运行typeof

obj = { xxx: false }

typeof obj.xxx // 'boolean'
typeof obj.yyy // 'undefined'

I ended up doing a review of my code to figure out overall that I had some mix matched cases.我最终对我的代码进行了审查,以找出总体上我有一些混合匹配的情况。 While I was in all doing what I should have, I overwrote one of my variables and caused the object I was looking for to essentially to end up going missing.当我在做我应该做的所有事情时,我覆盖了我的一个变量并导致我正在寻找的对象基本上最终丢失了。 So in fact false was correct.所以事实上false是正确的。

So to verify the how or which was proper for me in my case.因此,要验证在我的情况下如何或哪种方式适合我。

obj.hasOwnProperty([theScope+'_enabled']);

was the proper way.是正确的方法。

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

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