mozilla

Compare Revisions

Object.keys()

Change Revisions

Revision 15261:

Revision 15261 by dbruant on

Revision 15262:

Revision 15262 by olivernn on

Title:
keys
keys
Slug:
JavaScript/Reference/Global_Objects/Object/keys
JavaScript/Reference/Global_Objects/Object/keys
Tags:
javascript, "JavaScript 1.8.5", ECMAScript5
javascript, "JavaScript 1.8.5", ECMAScript5
Content:

Revision 15261
Revision 15262
n92if(!Object.keys) Object.keys = function(o){n92if (!Object.keys) {
93 if (o !== Object(o))93  Object.keys = (function () {
94      throw new TypeError('Object.keys called on non-object');94    var hasOwnProperty = Object.prototype.hasOwnProperty,
95 var ret=[],p;95        hasDontEnumBug = !({toString: null}).propertyIsEnumerable
 >('toString'),
96 for(p in o) if(Object.prototype.hasOwnProperty.call(o,p)) ret.pu96        dontEnums = [
>sh(p); 
97 return ret;97          'toString',
98}98          'toLocaleString',
99          'valueOf',
100          'hasOwnProperty',
101          'isPrototypeOf',
102          'propertyIsEnumerable',
103          'constructor'
104        ],
105        dontEnumsLength = dontEnums.length
106 
107    return function (obj) {
108      if (typeof obj !== 'object' && typeof obj !== 'func
 >tion' || obj === null) throw new TypeError('Object.keys called on
 > non-object')
109 
110      var result = []
111 
112      for (var prop in obj) {
113        if (hasOwnProperty.call(obj, prop)) result.push(prop)
114      }
115 
116      if (hasDontEnumBug) {
117        for (var i=0; i < dontEnumsLength; i++) {
118          if (hasOwnProperty.call(obj, dontEnums[i])) result.push
 >(dontEnums[i])
119        }
120      }
121      return result
122    }
123  })()
124};
n104      Based on <a class=" external" href="http://kangax.github.con130      Based on <a class="external" href="http://kangax.github.com
>m/es5-compat-table/">Kangax's compat table</a>.>/es5-compat-table/">Kangax's compat table</a>.
t215        <a class=" link-https" href="https://bugzilla.mozilla.orgt241        <a class="link-https" href="https://bugzilla.mozilla.org/
>/show_bug.cgi?id=307791">Bug&nbsp;307791</a> - <span id="summary_>show_bug.cgi?id=307791">Bug&nbsp;307791</a> - <span id="summary_a
>alias_container"><span id="short_desc_nonedit_display">Implement >lias_container"><span id="short_desc_nonedit_display">Implement E
>ES5's Object.keys(O)</span></span>>S5's Object.keys(O)</span></span>

Back to History