Skip to content

Instantly share code, notes, and snippets.

@minodisk
Created October 18, 2011 20:07
Show Gist options
  • Save minodisk/1296555 to your computer and use it in GitHub Desktop.
Save minodisk/1296555 to your computer and use it in GitHub Desktop.
`for in String` runs without throwing Error
var _keys = function(obj){
//if(Object.keys) return Object.keys(obj);
var keys = [];
for(var k in obj){
if(obj.hasOwnProperty(k)) keys.push(k);
}
return keys;
};
_keys('foo'); // [0, 1, 2]
var _keys = function(obj){
//if(Object.keys) return Object.keys(obj);
if (typeof obj != 'object' && typeof obj != 'function') {
throw new TypeError('-');
}
var keys = [];
for(var k in obj){
if(obj.hasOwnProperty(k)) keys.push(k);
}
return keys;
};
try {
alert(_keys('foo'));
} catch (err) {
alert(err); // TypeError: -
}
@minodisk
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment