-
-
Save atk/1034464 to your computer and use it in GitHub Desktop.
polyfill an ES5-compatibile Object.keys where needed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object.keys = Object.keys || | |
function ( | |
o, // object | |
k, // key | |
r // result array | |
){ | |
// initialize object and result | |
r=[]; | |
// iterate over object keys | |
for (k in o) | |
// fill result array with non-prototypical keys | |
r.hasOwnProperty.call(o, k) && r.push(k); | |
// return result | |
return r | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object.keys=Object.keys||function(o,k,r){r=[];for(k in o)r.hasOwnProperty.call(o,k)&&r.push(k);return r} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Alex Kloss <[email protected]> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "objkeys", | |
"description": "polyfill an ES5-compatibile Object.keys where needed.", | |
"keywords": [ | |
"object", | |
"keys", | |
"es5", | |
"polyfill" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<title>Foo</title> | |
<div>Expected value: <b>test, a, b, c</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
var testdata = { test: 1, a: 2, b: 3, c: 4 }; | |
var keys = Object.keys=Object.keys||function(o,k,r){r=[];for(k in o)r.hasOwnProperty.call(o,k)&&r.push(k);return r} | |
document.getElementById( "ret" ).innerHTML = keys(testdata); | |
</script> |
Object.keys=Object.keys||function(o,r){r=[];for(r[r.length] in o);return r}
for(r[r.length] in o);
- this is Beautiful! but we have to use hasOwnProperty...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To be ES5 compatible it would also need to throw an error when
o
is not an object/function.