The content on this page is OBSOLETE

Standardize some useful but missing built-in methods on Object:

Object.getPropertyDescriptor(obj, name) -> pd
Object.getPropertyNames(obj) -> [ string ]

Object.getPropertyDescriptor behaves like Object.getOwnPropertyDescriptor, except that it also takes the object’s prototype chain into account. The same holds true for getPropertyNames, which combines all own with all inherited property names (removing duplicates in the process).

Non-normative Implementation

Object.getPropertyDescriptor = function(subject, name) {
  var pd = Object.getOwnPropertyDescriptor(subject, name);
  var proto = Object.getPrototypeOf(subject);
  while (pd === undefined && proto !== null) {
    pd = Object.getOwnPropertyDescriptor(proto, name);
    proto = Object.getPrototypeOf(proto);
  return pd;
Object.getPropertyNames = function(subject, name) {
  var props = Object.getOwnPropertyNames(subject);
  var proto = Object.getPrototypeOf(subject);
  while (proto !== null) {
    props = props.concat(Object.getOwnPropertyNames(proto));
    proto = Object.getPrototypeOf(proto);
  // FIXME: remove duplicate property names from props
  return props;

Interaction with Proxies

Under the new direct proxies API, these methods would _not_ trigger a corresponding trap on the handler. Instead, these methods trigger the getOwnPropertyDescriptor or getOwnPropertyNames traps of a proxy in the prototype chain.

Under the old proxies API, these methods would trigger a corresponding trap, so that proxies could emulate inherited properties. At the November 2011 TC39 meeting, we decided that proxies don’t necessarily need to be able to do this kind of emulation, since any new proto-climbing algorithms written by end-users will also not have a chance to “delegate” the proto-walking to an encountered proxy. Instead, these algorithms climb the proto chain themselves, interacting only with the “own” properties at each level of the proto chain.

See also

harmony/extended_object_api.txt · Last modified: 2013/10/21 14:12 by tomvc
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki