Test for existence of nested JavaScript object key

If I have a reference to an object: var test = {}; that will potentially (but not immediately) have nested objects, something like: {level1: {level2: {level3: “level3”}}}; What is the best way to check for the existence of property in deeply nested objects? alert(test.level1); yields undefined, but alert(test.level1.level2.level3); fails. I’m currently doing something like this: … Read more

Dynamically access object property using variable

I’m trying to access a property of an object using a dynamic name. Is this possible? const something = { bar: “Foobar!” }; const foo = ‘bar’; something.foo; // The idea is to access something.bar, getting “Foobar!” 18 s 18 There are two ways to access properties of an object: Dot notation: something.bar Bracket notation: … Read more

How does the @property decorator work in Python?

I would like to understand how the built-in function property works. What confuses me is that property can also be used as a decorator, but it only takes arguments when used as a built-in function and not when used as a decorator. This example is from the documentation: class C: def __init__(self): self._x = None … Read more

How does the @property decorator work in Python?

I would like to understand how the built-in function property works. What confuses me is that property can also be used as a decorator, but it only takes arguments when used as a built-in function and not when used as a decorator. This example is from the documentation: class C: def __init__(self): self._x = None … Read more

How to efficiently count the number of keys/properties of an object in JavaScript

What’s the fastest way to count the number of keys/properties of an object? Is it possible to do this without iterating over the object? I.e., without doing: var count = 0; for (k in myobj) if (myobj.hasOwnProperty(k)) ++count; (Firefox did provide a magic __count__ property, but this was removed somewhere around version 4.) 20 20 … Read more

What’s the difference between the atomic and nonatomic attributes?

What do atomic and nonatomic mean in property declarations? @property(nonatomic, retain) UITextField *userName; @property(atomic, retain) UITextField *userName; @property(retain) UITextField *userName; What is the operational difference between these three? 2 27 The last two are identical; “atomic” is the default behavior (note that it is not actually a keyword; it is specified only by the absence … Read more