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

查看:24
本文介绍了检查 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 是一个有效的对象,但又想让你的代码防弹以防万一't,那么在它周围放置一个 try catch 并让一个错误处理程序捕获任何丢失的段可能是最干净的.然后,您可以只使用一个 if 条件而不是四个来检测终端属性是否存在,如果中间对象不存在,则可以使用一个捕获处理程序来捕获事物:

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.我发现有时我可以用一个更大的功能块周围的单个 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.

仅供参考,如果通常的操作是不使用 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天全站免登陆