473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Prototypes and enumerable properties

I'd like to add a method foo() to all objects:

Object.prototype.foo=function() {
// whatever
}

The caveat, though, is that I do not want foo to be enumerable by a
for-in loop. Can I do that? If not, is there a way to derive a
subclass of Object for which foo is not enumerable?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #1
1 1909
> I'd like to add a method foo() to all objects:

Object.prototype.foo=function() {
// whatever
}

The caveat, though, is that I do not want foo to be enumerable by a
for-in loop. Can I do that? If not, is there a way to derive a
subclass of Object for which foo is not enumerable?


No. I think this was a design error in the language itself. The
JavaScript VM has a DONTENUM property, but it not available to the
programmer.

The best you can do is exclude functions while enumerating.

for (key in object) {
if (typeof object[key] != 'function') {
do your thing
}
}

http://www.crockford.com/javascript
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Michele Simionato | last post by:
So far, I have not installed Prothon, nor I have experience with Io, Self or other prototype-based languages. Still, from the discussion on the mailing list, I have got the strong impression that...
7
by: junky_fellow | last post by:
Can a function have two different prototypes ? If not , then how can main() have two different prototypes ? int main(void) and int main argc(int argc, char *argv) I mean to say, if I declare...
4
by: petermichaux | last post by:
Hi, A discussion on the YUI page is depending on some speculation and I know someone here knows the truth. Below is the relevant section. If you can clarify this confusion I would greatly...
1
by: petermichaux | last post by:
Hi, I have searched the archives but didn't find the questions and answers I am looking for. I have been looking at Prototype.js quite a bit lately as I need to create a very small library of...
2
by: Jeff Stewart | last post by:
I'm curious about some of the machinery behind prototypal inheritance. What's going on under JavaScript's hood that prevents me from assigning properties to the prototype of an Object proper,...
1
by: Hermann Klinke | last post by:
This is what I mean: I have the an enumerable object (implements IEnumerable =GetEnumerator() method) and I would like to find out what types underly this enumerable object. For example, if I have...
73
by: Steph Barklay | last post by:
Hi, I'm currently taking a data structures course in C, and my teacher said that function prototypes are not allowed in any of our code. He also said that no professional programmers use function...
1
by: dhtmlkitchen | last post by:
I think this is not possible... var fnProto = Function().prototype; var badAss = { constructor : "Chuck Norris" }; result = ; function isConstructorEnumerable( ocean, pacific ) {
5
by: Anna Smidt | last post by:
I have moved this to a new topic now. I think I'm going crazy: I have searched for 2 hours now where the actual code is that implements these functions. Without success. I have looked at all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.