Connecting Tech Pros Worldwide Forums | Help | Site Map

reading object properties

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,664
#1: Oct 9 '09
is there a simple and elegant way to get only the properties and not the methods of a Javascript object?

sample
Expand|Select|Wrap|Line Numbers
  1. Object.prototype.aMethod = function() { … }
  2. var sample = { name: "value" };
if I use for … in here, I will also get aMethod(), which I have to filter off in the loop…

gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#2: Oct 12 '09

re: reading object properties


you could use a:

Expand|Select|Wrap|Line Numbers
  1. typeof prop != 'function'
check for that purpose ... i'm not aware of another possibility ...

kind regards
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,664
#3: Oct 12 '09

re: reading object properties


Quote:

Originally Posted by gits View Post

... i'm not aware of another possibility ...

what a pity, but I guess it can’t be helped.
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#4: Oct 12 '09

re: reading object properties


:) ... the typeof check looks a bit ugly ... but we always use it for the purpose you mentioned ... and for a second look ... it looks better then, since it just works :)
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,664
#5: Oct 12 '09

re: reading object properties


I too couldn’t think of a better filter…
Reply