JavaScript: Property, Prototype Chain, Enumerable: Accessing & Listing Properties

, , …,

This page gives a summary of operations on object properties. Some will travese the prototype chain, some not, some depend on whether the property's enumerable attribute is true. 〔☛ JavaScript Property's Attributes: Writable, Enumerable, Configurable

First, remember that in JavaScript, each object has a parent (called its prototype), and parent also has parent, forming a prototype chain. When looking up a property, it'll check the object's parent's properties (and therefore all the properties in prototype chain). 〔☛ JavaScript Tutorial: Understanding Prototype & Inheritance

Property Operations: Access, Set, Delete, Check Existence

Here's a summary of operations on property.

JavaScript property operations
syntaxmeaningproto chain
o["p"]Return the value of the property.yes
o["p"] = valueModify or create a new property for the object.no
delete o["p"]Delete a property p in o if it exists.no
o.hasOwnProperty("p")true if the object has the property.no
p in otrue if is own property or in prototype chain.yes

〔☛ JavaScript: Dot Notation vs Bracket Notation for Accessing Properties〕 〔☛ JavaScript Tutorial: Understanding Prototype & Inheritance

Listing & Enumerating Properties

Here's a summary of ways to list or loop thru properties.

JavaScript property listing operations
syntaxmeaningproto chainenumerable only
Object.getOwnPropertyNames(o)Return a array of all own property names. (including non-enumerable ones)nono
Object.keys(o)Return a array of property names that are o's own properties, and their “enumerable” attributes are true.noyes
for (var x in o) {…}loop thru all enumerable properties that is o's own properties or is in the prototype chain of o, set variable name x to it. Order is not guaranteed.yesyes

〔☛ JavaScript Array Methods

blog comments powered by Disqus