简体   繁体   English

我可以使用'in'关键字来测试(树)对象中的属性吗

[英]Can I use the 'in' keyword to test a property in an (tree) object

Let's say I've got the following object: 假设我有以下对象:

Variables.settings.backend.url = 'http://localhost';

What I would do to test is url is available, is do to the following: 我要测试的是url是否可用,请执行以下操作:

if ('settings' in Variables && 'backend' in Variables.settings && 'url' in Variables.settings.backend) {
  // true
}

This is quite cumbersome. 这很麻烦。

If this was PHP, i could just do 如果这是PHP,我可以做

if (!empty($variables['settings']['backend']['url']) {
  //true
}

Can this be done any simpler in JS? 可以用JS更简单吗?

I wrote a function to test this : 我写了一个函数来测试这个:

var isModuleDefined = function(moduleName) {
    var d = moduleName.split(".");
    var md = d[0];
    for (var i = 1, len = d.length; i <= len; i++) {
        if (eval("typeof " + md) !== "undefined") {
            md = md + "." + d[i];
            continue;
        }
        break;
    }
    return i === len + 1;
};

isModuleDefined("Variables.settings.backend.url");

but i really don't know about the cost-efficiency of that method, using eval. 但是我真的不知道使用eval的那种方法的成本效益。

Edit (Without eval..): 编辑 (无评估):

var isModuleDefined = function(moduleName) {
    var d = moduleName.split(".");
    var base = window;
    for (var i = 0, len = d.length; i < len; i++) {
        if (typeof base[d[i]] != "undefined") {
            base = base[d[i]];
            continue;
        }
        break;
    }
    return i === len;
};

Yet another variant 另一个变体

function isFieldExist(expression){
    var fields = expression.split('.'),
        cur = this;

    for(var i=0; i<fields.length; i++){
        if(typeof cur[fields[i]] === "undefined") return false;
        cur = cur[fields[i]];
    }
    return true;    
}

for using 用于

isFieldExist("Variables.settings.backend.url"); // find in global scope

isFieldExist.call(Variables, "settings.backend.url"); // find in Variables

暂无
暂无

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

相关问题 我可以使用方括号中的表达式通过“this”关键字访问 class 的属性吗? - Can I use expression in square bracket to access property of class by "this" keyword? 我可以使用对象属性值作为CSS属性值吗? - can i use an object property value, as a css property value? 如何使用来自 class 的 static getter 并调用 object 并使用 this 关键字? - How can I use a static getter from a class and call an object and use the this keyword? 如何使用变量值调用 object 属性? - How can i use a variable value to call an object property? 如何使用字符串访问对象属性? - How can I use a string to access an object property? 如何使用单个变量使用 object 属性? - How can I use the a object property using single variable? 我可以使用`obj.constructor === Array`来测试对象是否是数组? - Can I use `obj.constructor === Array` to test if object is Array? 我可以使用哪些搜索选项来限制marklogic搜索API关键字搜索以不搜索提到的json属性值? - What search options I can use to restrict the marklogic search API keyword search to not search in mentioned json property values? 为什么当我在此JavaScript对象上使用反射时,看不到其原型对象中定义的属性? - Why when I use reflection on this JavaScript object I can't see a property defined in its prototype object? 当我声明它时,如何在同一对象的另一个属性中使用前一个对象属性的值 - How can I use the value of a previous object property inside another property of the same object when I am declaring it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM