我正在检查对象属性是否存在,其中的变量包含相关属性名称。
var myObj;
myObj.prop = "exists";
var myProp = "p"+"r"+"o"+"p";
if(myObj.myProp){
    alert("yes, i have that property");
};
这是undefined因为它正在寻找myObj.myProp但我希望它检查myObj.prop
我正在检查对象属性是否存在,其中的变量包含相关属性名称。
var myObj;
myObj.prop = "exists";
var myProp = "p"+"r"+"o"+"p";
if(myObj.myProp){
    alert("yes, i have that property");
};
这是undefined因为它正在寻找myObj.myProp但我希望它检查myObj.prop
var myProp = 'prop';
if(myObj.hasOwnProperty(myProp)){
    alert("yes, i have that property");
}
或者
var myProp = 'prop';
if(myProp in myObj){
    alert("yes, i have that property");
}
或者
if('prop' in myObj){
    alert("yes, i have that property");
}
请注意,hasOwnProperty它不会检查继承的属性,而in会检查。例如'constructor' in myObj是真的,但myObj.hasOwnProperty('constructor')不是。
您可以使用hasOwnProperty,但根据参考,使用此方法时需要引号:
if (myObj.hasOwnProperty('myProp')) {
    // do something
}
另一种方法是使用in运算符,但这里也需要引号:
if ('myProp' in myObj) {
    // do something
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in
感谢大家的帮助和推动摆脱 eval 语句。变量需要在括号中,而不是点符号。这有效并且是干净,正确的代码。
每一个都是变量:appChoice、underI、underObstr。
if(typeof tData.tonicdata[appChoice][underI][underObstr] !== "undefined"){
    //enter code here
}
对于自有财产:
var loan = { amount: 150 };
if(Object.prototype.hasOwnProperty.call(loan, "amount")) 
{ 
   //will execute
}
注意:使用Object.prototype.hasOwnProperty比 Loan.hasOwnProperty(..) 好,以防在原型链中定义了自定义 hasOwnProperty(这里不是这种情况),例如
var foo = {
      hasOwnProperty: function() {
        return false;
      },
      bar: 'Here be dragons'
    };
要在发现中包含继承的属性,请使用in运算符:(但您必须将一个对象放在 'in' 的右侧,原始值将抛出错误,例如 'home' 中的 'length'将抛出错误,但'length'在 new String('home') 中不会)
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
}
检查对象上是否存在属性的更安全的方法是使用空对象或对象原型来调用 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