javascript - How to fix missing keys in Object.keys() compared to for...in with hasOwnProperty() -


in browsers (chrome, safari), object.keys() doesn't return keys for-in loop hasownproperty() returns.

is there workaround without using for-in loops ?

also there object window exhibits same bug or problem windowobject tests tend show ?

clarification

both should return only own , enumerable properties:

conclusion: should iterate same keys.

browsers results

1) firefox 39: no missing key

2) chromium 38: 47 missing keys:

["speechsynthesis", "localstorage", "sessionstorage", "applicationcache", "webkitstorageinfo", "indexeddb", "webkitindexeddb", "crypto", "css", "performance", "console", "devicepixelratio", "stylemedia", "parent", "opener", "frames", "self", "defaultstatus", "defaultstatus", "status", "name", "length", "closed", "pageyoffset", "pagexoffset", "scrolly", "scrollx", "screentop", "screenleft", "screeny", "screenx", "innerwidth", "innerheight", "outerwidth", "outerheight", "offscreenbuffering", "frameelement", "clientinformation", "navigator", "toolbar", "statusbar", "scrollbars", "personalbar", "menubar", "locationbar", "history", "screen"] 

3) safari 5.1: 37 missing keys:

["open", "moveby", "find", "resizeto", "cleartimeout", "btoa", "getcomputedstyle", "settimeout", "scrollby", "print", "resizeby", "atob", "opendatabase", "moveto", "scroll", "confirm", "getmatchedcssrules", "showmodaldialog", "close", "clearinterval", "webkitconvertpointfromnodetopage", "matchmedia", "prompt", "focus", "blur", "scrollto", "removeeventlistener", "postmessage", "setinterval", "getselection", "alert", "stop", "webkitconvertpointfrompagetonode", "addeventlistener", "dispatchevent", "captureevents", "releaseevents"] 

test script

var res = (function(obj) {     var hasown = object.prototype.hasownproperty;      var allkeys = [];     for(var key in obj) {         if(hasown.call(obj, key)) {             allkeys.push(key);         }     }      var keys = object.keys(obj);      var missingkeys = [];     for(var = 0; < allkeys.length; i++) {         if(keys.indexof(allkeys[i]) === -1) {             missingkeys.push(allkeys[i]);         }     }      return {allkeys: allkeys, keys: keys, missingkeys: missingkeys}; })(window);  // should empty if followings return same set of keys: // - for...in hasownproperty() // - object.keys() console.log(res.missingkeys, res.missingkeys.length); 


Comments