我正在检查是否存在一个对象属性,该属性包含一个变量,该变量包含有问题的属性名称。

var myObj;
myObj.prop = "exists";
var myProp = "p"+"r"+"o"+"p";

if(myObj.myProp){
    alert("yes, i have that property");
};

这是未定义的,因为它正在查找myObj.myrop,但我希望它检查myObj.rop


当前回答

感谢大家的帮助,并推动消除eval声明。变量需要放在括号中,而不是点符号。这是一个干净、正确的代码。

其中每个都是变量:appChoice、underI、underObst。

if(typeof tData.tonicdata[appChoice][underI][underObstr] !== "undefined"){
    //enter code here
}

其他回答

var myProp = 'prop';
if(myObj.hasOwnProperty(myProp)){
    alert("yes, i have that property");
}

Or

var myProp = 'prop';
if(myProp in myObj){
    alert("yes, i have that property");
}

Or

if('prop' in myObj){
    alert("yes, i have that property");
}

请注意,hasOwnProperty不检查继承的财产,而in检查继承的属性。例如,myObj中的“constructor”为true,但myObj.hasOwnProperty(“constructors”)不是。

对于自有财产:

var loan = { amount: 150 };
if(Object.prototype.hasOwnProperty.call(loan, "amount")) 
{ 
   //will execute
}

注意:如果在原型链中定义了自定义hasOwnProperty(这里不是这样),则使用Object.prototype.hasOwnProperty优于loan.hasOwn Property(..),例如

var foo = {
      hasOwnProperty: function() {
        return false;
      },
      bar: 'Here be dragons'
    };

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty

要在查找中包含继承的财产,请使用in运算符:(但必须将对象放置在“in”的右侧,基元值将引发错误,例如,“home”中的“length”将引发错误;但新String(“home“)中的“longth”不会)

const yoshi = { skulk: true };
const hattori = { sneak: true };
const kuma = { creep: true };
if ("skulk" in yoshi) 
    console.log("Yoshi can skulk");

if (!("sneak" in yoshi)) 
    console.log("Yoshi cannot sneak");

if (!("creep" in yoshi)) 
    console.log("Yoshi cannot creep");

Object.setPrototypeOf(yoshi, hattori);

if ("sneak" in yoshi)
    console.log("Yoshi can now sneak");
if (!("creep" in hattori))
    console.log("Hattori cannot creep");

Object.setPrototypeOf(hattori, kuma);

if ("creep" in hattori)
    console.log("Hattori can now creep");
if ("creep" in yoshi)
    console.log("Yoshi can also creep");

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in

注意:人们可能会尝试使用typeof和[]属性访问器作为以下代码,但这些代码并不总是有效的。。。

var loan = { amount: 150 };

loan.installment = undefined;

if("installment" in loan) // correct
{
    // will execute
}

if(typeof loan["installment"] !== "undefined") // incorrect
{
    // will not execute
}

感谢大家的帮助,并推动消除eval声明。变量需要放在括号中,而不是点符号。这是一个干净、正确的代码。

其中每个都是变量:appChoice、underI、underObst。

if(typeof tData.tonicdata[appChoice][underI][underObstr] !== "undefined"){
    //enter code here
}

为我工作。

if (typeof receviedData?.d?.heartbeat_interval != "undefined") {
}

检查对象上是否存在属性的更安全的方法是使用空对象或对象原型来调用hasOwnProperty()

var foo = {
  hasOwnProperty: function() {
    return false;
  },
  bar: 'Here be dragons'
};

foo.hasOwnProperty('bar'); // always returns false

// Use another Object's hasOwnProperty and call it with 'this' set to foo
({}).hasOwnProperty.call(foo, 'bar'); // true

// It's also possible to use the hasOwnProperty property from the Object
// prototype for this purpose
Object.prototype.hasOwnProperty.call(foo, 'bar'); // true

来自MDN Web Docs的引用-Object.protype.hasOwnProperty()