Skip to content

Instantly share code, notes, and snippets.

@davestewart
Created August 18, 2011 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davestewart/1154880 to your computer and use it in GitHub Desktop.
Save davestewart/1154880 to your computer and use it in GitHub Desktop.
prototype + for..in demo
// 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