Created
October 18, 2011 20:07
-
-
Save minodisk/1296555 to your computer and use it in GitHub Desktop.
`for in String` runs without throwing Error
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
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] |
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
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: - | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
run in jsFiddle