Created
August 18, 2011 19:08
-
-
Save davestewart/1154880 to your computer and use it in GitHub Desktop.
prototype + for..in demo
This file contains hidden or 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
// modify Object.prototype | |
Object.prototype.property = 'NEW PROPERTY'; | |
// decalre datatypes | |
var datatypes = | |
[ | |
'hello', | |
1, | |
new Date(), | |
{a:1, b:2, c:3}, | |
[1,2,3], | |
true | |
]; | |
// loop | |
for(var i = 0; i < datatypes.length; i++) | |
{ | |
var obj = datatypes[i]; | |
fl.trace('\n' + obj + ' (' +typeof obj+ ')'); | |
for(var name in obj) | |
{ | |
// should otherwise add in a check for obj.hasOwnProperty(name) | |
// but the problem is that it will break any other scripts that | |
// DON'T have this check, esp those that are using for..in where | |
// they should be using for... the Flash IK tools are a case | |
// in point! | |
fl.trace(' > ' + name + ':' + obj[name]); | |
} | |
} | |
// results | |
/* | |
hello (string) | |
> 0:h | |
> 1:e | |
> 2:l | |
> 3:l | |
> 4:o | |
> property:NEW PROPERTY | |
1 (number) | |
> property:NEW PROPERTY | |
Thu Aug 18 2011 20:07:52 GMT+0100 (GMT Daylight Time) (object) | |
> property:NEW PROPERTY | |
[object Object] (object) | |
> a:1 | |
> b:2 | |
> c:3 | |
> property:NEW PROPERTY | |
1,2,3 (object) | |
> 0:1 | |
> 1:2 | |
> 2:3 | |
> property:NEW PROPERTY | |
true (boolean) | |
> property:NEW PROPERTY | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment