Notice: Some MDN email addresses and encrypted passwords were temporarily posted on a publicly accessible server. http://blog.mozilla.org/security/2014/08/01/mdn-database-disclosure/

Your Search Results

    Object.getOwnPropertyNames()

    Summary

    The Object.getOwnPropertyNames() method returns an array of all properties (enumerable or not) found directly upon a given object.

    Syntax

    Object.getOwnPropertyNames(obj)

    Parameters

    obj
    The object whose enumerable and non-enumerable own properties are to be returned.

    Description

    Object.getOwnPropertyNames returns an array whose elements are strings corresponding to the enumerable and non-enumerable properties found directly upon obj. The ordering of the enumerable properties in the array is consistent with the ordering exposed by a for...in loop (or by Object.keys) over the properties of the object. The ordering of the non-enumerable properties in the array, and among the enumerable properties, is not defined.

    Examples

    var arr = ["a", "b", "c"];
    print(Object.getOwnPropertyNames(arr).sort()); // prints "0,1,2,length"
    
    // Array-like object
    var obj = { 0: "a", 1: "b", 2: "c"};
    print(Object.getOwnPropertyNames(obj).sort()); // prints "0,1,2"
    
    // Printing property names and values using Array.forEach
    Object.getOwnPropertyNames(obj).forEach(function(val, idx, array) {
      print(val + " -> " + obj[val]);
    });
    // prints
    // 0 -> a
    // 1 -> b
    // 2 -> c
    
    // non-enumerable property
    var my_obj = Object.create({}, { getFoo: { value: function() { return this.foo; }, enumerable: false } });
    my_obj.foo = 1;
    
    print(Object.getOwnPropertyNames(my_obj).sort()); // prints "foo, getFoo"
    

    If you want only the enumerable properties, see Object.keys or use a for...in loop (although note that this will return enumerable properties not found directly upon that object but also along the prototype chain for the object unless the latter is filtered with hasOwnProperty()).

    Items on the prototype chain are not listed:

    function ParentClass () {
    }
    ParentClass.prototype.inheritedMethod = function () {
    };
    
    function ChildClass () {
      this.prop = 5;
      this.method = function () {};
    }
    ChildClass.prototype = new ParentClass;
    ChildClass.prototype.prototypeMethod = function () {
    };
    
    alert(
      Object.getOwnPropertyNames(
        new ChildClass() // ["prop", "method"]
      )
    )
    

    Get Non-Enumerable Only

    This uses the Array.prototype.filter function to remove the enumerable keys (obtained with Object.keys) from a list of all keys (obtained with Object.getOwnPropertynames) leaving only the non-enumerable keys.

    var target = myObject;
    var enum_and_nonenum = Object.getOwnPropertyNames(target);
    var enum_only = Object.keys(target);
    var nonenum_only = enum_and_nonenum.filter(function(key) {
      var indexInEnum = enum_only.indexOf(key)
      if (indexInEnum == -1) {
        //not found in enum_only keys mean the key is enumerable, so return true so we keep this in the filter
        return true;
      } else {
        return false;
      }
    });
                
    console.log(nonenum_only);
    

    Specifications

    Specification Status Comment
    ECMAScript 5.1 (ECMA-262)
    The definition of 'Object.getOwnPropertyNames' in that specification.
    Standard Initial definition.
    Implemented in JavaScript 1.8.5
    ECMAScript 6 (ECMA-262)
    The definition of 'Object.getOwnPropertyNames' in that specification.
    Draft  

    Browser compatibility

    Feature Firefox (Gecko) Chrome Internet Explorer Opera Safari
    Basic support 4 (2.0) 5 9 12 5
    Feature Firefox Mobile (Gecko) Android IE Mobile Opera Mobile Safari Mobile
    Basic support ? ? ? ? ?

    Based on Kangax's compat table.

    SpiderMonkey-specific notes

    • Prior to SpiderMonkey 28 (Firefox 28 / Thunderbird 28 / SeaMonkey 2.25), Object.getOwnPropertyNames did not see unresolved properties of Error objects. This has been fixed in later versions (bug 724768).

    See also

    Document Tags and Contributors

    Last updated by: Noitidart,