检查JavaScript中深层嵌套对象属性是否存在的最简单方法是什么? [英] What's the simplest approach to check existence of deeply-nested object property in JavaScript?

查看:109
本文介绍了检查JavaScript中深层嵌套对象属性是否存在的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须检查深层嵌套的对象属性,例如 YAHOO.Foo.Bar.xyz

I have to check deeply-nested object property such as YAHOO.Foo.Bar.xyz.

我目前的代码使用是

if (YAHOO && YAHOO.Foo && YAHOO.Foo.Bar && YAHOO.Foo.Bar.xyz) {
    // operate on YAHOO.Foo.Bar.xyz
}

这有效,但看起来很笨拙。

This works, but looks clumsy.

有没有更好的方法来检查这种深层嵌套的属性?

Is there any better way to check such deeply nested property?

推荐答案

如果您希望 YAHOO.Foo.Bar 成为有效对象,但希望使您的代码具有防弹功能以防它不是,那么只需在它周围放一个try catch并让一个错误处理程序捕获任何丢失的段就可以是最干净的。然后,您可以使用一个 if 条件而不是四个将检测终端属性是否存在,以及一个catch处理程序,以便在中间对象不存在时捕获事物: / p>

If you expect YAHOO.Foo.Bar to be a valid object, but want to make your code bulletproof just in case it isn't, then it can be cleanest to just put a try catch around it and let one error handler catch any missing segment. Then, you can just use one if condition instead of four that will detect if the terminal property exists and a catch handler to catch things if the intermediate objects don't exist:

try {
    if (YAHOO.Foo.Bar.xyz) {
        // operate on YAHOO.Foo.Bar.xyz
} catch(e) {
    // handle error here
}

或者,根据代码的工作原理,它甚至可能就是这样:

or, depending upon how your code works, it might even just be this:

try {
    // operate on YAHOO.Foo.Bar.xyz
} catch(e) {
    // do whatever you want to do when YAHOO.Foo.Bar.xyz doesn't exist
}

我特别在处理应该是特定的外国输入时使用这些格式,但无效的输入是我想捕捉和处理自己而不是让异常向上传播的可能性。

I particularly use these when dealing with foreign input that is supposed to be of a particular format, but invalid input is a possibility that I want to catch and handle myself rather than just letting an exception propagate upwards.

一般来说,一些javascript开发人员使用不足/抓住。我发现有时候我可以用更大的功能块周围的单个try / catch替换5-10 if语句检查输入,并使代码更简单,更可读。显然,如果这是合适的,取决于特定的代码,但它绝对值得考虑。

In general, some javascript developers under-use try/catch. I find that I can sometimes replace 5-10 if statements checking input with a single try/catch around a larger function block and make the code a lot simpler and more readable at the same time. Obviously, when this is appropriate depends upon the particular code, but it's definitely worth considering.

FYI,如果通常的操作是不抛出try / catch的异常,它可以比一堆if语句快得多。

FYI, if the usual operation is to not throw an exception with the try/catch, it can be a lot faster than a bunch of if statements too.

如果你不想使用异常处理程序,您可以创建一个函数来为您测试任意路径:

If you don't want to use the exception handler, you can create a function to test any arbitrary path for you:

function checkPath(base, path) {
    var current = base;
    var components = path.split(".");
    for (var i = 0; i < components.length; i++) {
        if ((typeof current !== "object") || (!current.hasOwnProperty(components[i]))) {
            return false;
        }
        current = current[components[i]];
    }
    return true;
}

用法示例:

var a = {b: {c: {d: 5}}};
if (checkPath(a, "b.c.d")) {
    // a.b.c.d exists and can be safely accessed
}

这篇关于检查JavaScript中深层嵌套对象属性是否存在的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆