读取未定义的对象属性时强制JavaScript异常/错误? [英] Force JavaScript exception/error when reading an undefined object property?

查看:134
本文介绍了读取未定义的对象属性时强制JavaScript异常/错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次使用Javascript工作的有经验的C ++ / Java程序员。我使用Chrome作为浏览器。

I'm an experienced C++/Java programmer working in Javascript for the first time. I'm using Chrome as the browser.

我用字段和方法创建了几个Javascript类。当我读取一个不存在的对象字段时(由于我的错字),Javascript运行时不会抛出错误或异常。显然这样的读取字段是未定义的。例如:

I've created several Javascript classes with fields and methods. When I read an object's field that doesn't exist (due to a typo on my part), the Javascript runtime doesn't throw an error or exception. Apparently such read fields are 'undefined'. For example:

var foo = new Foo();
foo.bar = 1;
var baz = foo.Bar; // baz is now undefined

我知道我可以检查undefined是否与在JavaScript中检测未定义的对象属性,但这似乎很乏味因为我经常在我的代码中读取对象字段。

I know that I can check for equality against 'undefined' as mentioned in "Detecting an undefined object property in JavaScript", but that seems tedious since I read from object fields often in my code.

当我读取未定义的属性时,有没有办法强制抛出错误或异常? / strong>

Is there any way to force an error or exception to be thrown when I read an undefined property?

为什么在读取未定义的变量时抛出异常(与未定义的对象属性相反)?

And why is an exception thrown when I read an undefined variable (as opposed to undefined object property)?

推荐答案

这看起来像是一个尝试将一种语言塞进另一种语言范例的经典案例 - 更好的恕我直言,改变你的编码风格来遵循Javascript的做法而不是尝试使它符合C ++的概念和期望。

This looks to me like a classic case of trying to shoehorn one language into the paradigms of another - better IMHO to change your coding style to follow how Javascript does things than try to make it conform to C++ concepts and expectations.

这就是说,如果你想按照你的建议抛出错误,您需要在您尝试访问的对象或全局范围内定义某种自定义 getProperty 函数。实现可能如下所示:

That said, if you want to throw an error as you suggest, you'll need to define some sort of custom getProperty function, either on the object you're trying to access or in the global scope. An implementation might look like this:

function getProperty(o, prop) {
    if (o.hasOwnProperty(prop)) return o[prop];
    else throw new ReferenceError('The property ' + prop + 
        ' is not defined on this object');
}

var o = {
    foo: 1,
    bar: false,
    baz: undefined
};

getProperty(o, 'foo'); // 1
getProperty(o, 'bar'); // false
getProperty(o, 'baz'); // undefined
getProperty(o, 'foobar'); 
// ReferenceError: The property baz is not defined on this object

但这很难看,现在你在所有代码中都有这个自定义语言构造,使其不那么便携(例如,如果你想将代码的任何部分复制到另一个脚本中,你也必须复制你的新函数)并且对其他程序员不太清晰。所以我真的建议你在Javascript范例内工作并检查 undefined ,然后再访问你需要的属性(或者设置你的代码,这样就可以得到假y值了。不要破坏东西。

But this is ugly, and now you've got this custom language construct in all of your code, making it less portable (if, for example, you wanted to copy any part of your code into another script, you'd have to copy your new function too) and less legible to other programmers. So I'd really recommend working within the Javascript paradigm and checking for undefined before accessing the properties you need (or setting up your code so that false-y values are expected and don't break things).

至于你的第二个问题,为什么Javascript会针对未定义的变量抛出错误而不会针对未定义的对象属性抛出错误,我无法给出任何更好的答案比因为这就是语言规范中的内容。 对象返回未定义未定义的属性名称,但未定义的变量引用会引发错误

As to your second question, why Javascript throws an error for undefined variables but not for undefined object properties, I can't give any better answer than "Because that's what's in the language specification." Objects return undefined for undefined property names, but undefined variable references throw an error.

这篇关于读取未定义的对象属性时强制JavaScript异常/错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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