简体   繁体   English

如何检查javascript对象是否具有某个属性

[英]How to check if a javascript object has a certain property

Suppose I have a javascript object like this: 假设我有一个像这样的javascript对象:

window.config
config.UI = {
        "opacity": {
            "_type": "float",
            "_tag": "input",
            "_value": "1",
            "_aka": "opacity",
            "_isShow":"1"
 }

How can I judge if the "opacity" object has a property named "_test"? 如何判断“不透明度”对象是否具有名为“_test”的属性? like 喜欢

var c=config.ui.opacity;
for(var i in c)
{
   //c[i]=="_test"?
}

How do I find out if it's assigned as well? 我怎么知道它是否也被分配了?

There are at least three ways to do this; 至少有三种方法可以做到这一点; which one you use is largely up to you and sometimes even a matter of style, though there are some substantive differences: 您使用哪一个很大程度上取决于您,有时甚至是风格问题,尽管存在一些实质性差异:

if..in

You can use if..in : 你可以使用if..in

if ("_test" in config.UI.opacity)

...because when used in a test (as opposed to the special for..in loop), in tests to see if the object or its prototype (or its prototype's prototype, etc.) has a property by that name. ...因为在测试中使用时(与特殊的for..in循环相反), in测试中查看对象或其原​​型(或其原型的原型等)是否具有该名称的属性。

hasOwnProperty

If you want to exclude properties from prototypes (it doesn't much matter in your example), you can use hasOwnProperty , which is a function all objects inherit from Object.prototype : 如果要从原型中排除属性(在您的示例中并不重要),可以使用hasOwnProperty ,这是一个函数,所有对象都从Object.prototype继承:

if (config.UI.opacity.hasOwnProperty("_test"))

Just retrieve it and check the result 只需检索它并检查结果

Finally, you can just retrieve the property (even if it doesn't exist) and the decide what to do with the result by looking at the result; 最后,您可以检索属性(即使它不存在),并通过查看结果来决定如何处理结果; if you ask an object for the value of a property it doesn't have, you'll get back undefined : 如果你向一个对象询问它没有的属性的值,你将得到undefined

var c = config.UI.opacity._test;
if (c) {
    // It's there and has a value other than undefined, "", 0, false, or null
}

or 要么

var c = config.UI.opacity._test;
if (typeof c !== "undefined") {
    // It's there and has a value other than undefined
}

Being defensive 保守

If it's possible that config.UI won't have an opacity property at all, you can make all of those more defensive: 如果config.UI可能根本没有opacity属性,那么你可以使所有这些更具防御性:

// The if..in version:
if (config.UI.opacity && "_test" in config.UI.opacity)

// The hasOwnProperty version
if (config.UI.opacity && config.UI.opacity.hasOwnProperty("_test"))

// The "just get it and then deal with the result" version:
var c = config.UI.opacity && config.UI.opacity._test;
if (c) { // Or if (typeof c !== "undefined") {

That last one works because the && operator is particularly powerful in JavaScript compared with some other languages; 最后一个是有效的,因为与其他语言相比, &&运算符在JavaScript中特别强大; it's the corollary of the curiously-powerful || 这是奇怪的强大||的必然结果 operator . 运营商

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

相关问题 Javascript / InDesign:检查对象是否具有某些属性 - Javascript/InDesign: Check if Object has certain property 如果Object属性具有某些定界符,如何分割JavaScript数组元素 - How to Split JavaScript Array Elements if an Object property has certain delimitter Javascript:检查多维数组中的对象是否具有特定属性,然后删除该对象 - Javascript: check if an object within a multidimensional array has a certain property and then remove that object 如何检查 JavaScript 对象是否具有以特定字符串开头的属性名称? - How to check if a JavaScript Object has a property name that starts with a specific string? 如何检查 object 在 JavaScript 中是否具有特定属性? - How do I check if an object has a specific property in JavaScript? 如何检查数组是否具有具有特定属性的 object,然后找到该属性? - How do I check if an array has an object with a certain property and then find that property? JavaScript 中的对象具有属性深度检查 - Object has-property-deep check in JavaScript javascript-检查对象是否具有子属性 - javascript - check if object has child property 如何在JavaScript中检查对象属性 - How to check the object property in javascript JavaScript - 如果 object 属性具有特定值,则创建一个 object 键数组 - JavaScript - Create an array of object keys if the object property has certain value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM