4

Suppose I have a javascript object like this:

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"? 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?

1 Answer 1

14

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 ("_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.

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:

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:

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:

// 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; it's the corollary of the curiously-powerful || operator.

5
  • There's a gotcha with "just retrieving it" I just ran into: If you're digging deep into an object and one of the parent properties of the thing you're looking for is undefined, you'll get a TypeError, not 'undefined.'
    – worc
    Commented Aug 5, 2014 at 21:56
  • @worc: See the examples under "Being defensive" at the end. That's exactly the situation that constructs like var c = config.UI.opacity && config.UI.opacity._test; are for. Commented Aug 6, 2014 at 7:20
  • it's not exactly the problem i ran into. in my specific case an object like {{config}} was initialized but {{UI}} was undefined. so checking the {{UI}} fields {{opacity}} and {{_test}} fields ends up throwing an error rather than returning as undefined.
    – worc
    Commented Aug 7, 2014 at 5:03
  • @worc: So you just do what I showed above: var opacity = config && config.UI && config.UI.opacity; Commented Aug 7, 2014 at 9:10
  • yeah, i know. i was more pointing out the gotcha where an incomplete defense will throw errors rather than return undefined.
    – worc
    Commented Aug 8, 2014 at 17:46

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.