By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,605 Members | 2,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,605 IT Pros & Developers. It's quick & easy.

Object extensions becoming array content

P: n/a
Hi,

I've been running into something that strikes me as pretty annoying (esp
since I don't know how to work around it).
It concerns the extension of Object and the usage of Maps.

Following a snip I use to extend the Object object.

Object.prototype.extend = function (oSuper) {
for (sProperty in oSuper) {
this[sProperty] = oSuper[sProperty];
}
};

This all goes fine. The function available etc.
Here is the catch.

When I create ab arr=new Object() to be used as a Map or an arr=[] to be
used as a Map (as well) I can fill the arr. No problems so far.
When I use the for (o in arr) { ... } I keep on tripping over the the fact
that "extend" is a key with {ScriptFunction} as value.

Obviously this is *not* what I'm looking for.
Am I doing something wrong here and if so what?
Trying to rewrite my code every time this comes up is starting to ...

TIA

F DCG
Jul 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"F. Da Costa" <da*****@xs4all.nl> writes:
Following a snip I use to extend the Object object.

Object.prototype.extend = function (oSuper) {
for (sProperty in oSuper) {
this[sProperty] = oSuper[sProperty];
}
}; When I create ab arr=new Object() to be used as a Map or an arr=[] to
be used as a Map (as well) I can fill the arr. No problems so far.
When I use the for (o in arr) { ... } I keep on tripping over the the
fact that "extend" is a key with {ScriptFunction} as value.
Because it is. All your objects have "extend" as a property.
Obviously this is *not* what I'm looking for.
What are you looking for, then? (I.e., no, it's not obvious).

User added properties cannot be made non-enumerable, so anything you
add will show up in a for(in)-loop.
Am I doing something wrong here and if so what?


Try this:
---
Object.prototype.extend = function (oSuper) {
for (sProperty in oSuper) {
if (oSuper.hasOwnProperty(sProperty)) {
this[sProperty] = oSuper[sProperty];
}
}
};
---
Then you will only copy the non-inherited properties of oSuper.

"hasOwnProperty" is a fairly new feature in ECMAScript, so older
browsers won't understand it.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
Lasse Reichstein Nielsen wrote:
"F. Da Costa" <da*****@xs4all.nl> writes:

Following a snip I use to extend the Object object.

Object.prototype.extend = function (oSuper) {
for (sProperty in oSuper) {
this[sProperty] = oSuper[sProperty];
}
};
When I create ab arr=new Object() to be used as a Map or an arr=[] to
be used as a Map (as well) I can fill the arr. No problems so far.
When I use the for (o in arr) { ... } I keep on tripping over the the
fact that "extend" is a key with {ScriptFunction} as value.

Because it is. All your objects have "extend" as a property.

Ok, that's clear.

Obviously this is *not* what I'm looking for.

What are you looking for, then? (I.e., no, it's not obvious).

Just the verifiable content put in as data
User added properties cannot be made non-enumerable, so anything you
add will show up in a for(in)-loop. This is fine, so I test for this whilst looping through an array.

Example:
for (var o in arr) {
if (!!arr[o].someProp) { // the someProp *is* always in the data object
// arr[o] *is* a proper data entry
}
}

Would there be a better way to test for arr[o]=void (as per the Moz debugger)?
The only thing I could come up with was the !!
Am I doing something wrong here and if so what?

Try this:
---
Object.prototype.extend = function (oSuper) {
for (sProperty in oSuper) {
if (oSuper.hasOwnProperty(sProperty)) {
this[sProperty] = oSuper[sProperty];
}
}
};
---
Then you will only copy the non-inherited properties of oSuper.

"hasOwnProperty" is a fairly new feature in ECMAScript, so older
browsers won't understand it.

Thz but in that case I'll copy it into my js snips "to be inserted later".
First make it work ...

/L

F DCG
Jul 20 '05 #3

P: n/a
> I've been running into something that strikes me as pretty annoying (esp
since I don't know how to work around it).
It concerns the extension of Object and the usage of Maps.

Following a snip I use to extend the Object object.

Object.prototype.extend = function (oSuper) {
for (sProperty in oSuper) {
this[sProperty] = oSuper[sProperty];
}
};

This all goes fine. The function available etc.
Here is the catch.

When I create ab arr=new Object() to be used as a Map or an arr=[] to be
used as a Map (as well) I can fill the arr. No problems so far.
When I use the for (o in arr) { ... } I keep on tripping over the the fact
that "extend" is a key with {ScriptFunction} as value.

Obviously this is *not* what I'm looking for.
Am I doing something wrong here and if so what?
Trying to rewrite my code every time this comes up is starting to ...


I consider that to be a design error in the language. The ECMAScript model
includes a property called DONTENUM which hides properties from the for..in
statement. Unfortunately, there is nothing in the language that allows you to
use it. That bites.

I have used two alternatives:

(1) Have a .bag member in your function object which holds the enumerated stuff.

(2) In your for..in loop, reject function values

for (n in o) {
if (typeof o[n] != 'function') {
...
}
}

http://www.ecmascript.org

Jul 20 '05 #4

P: n/a
"F. Da Costa" <da*****@xs4all.nl> writes:
Just the verifiable content put in as data
I.e., Just the data you put there yourself, not the ones inherited from
the prototype.
User added properties cannot be made non-enumerable, so anything you
add will show up in a for(in)-loop.

This is fine, so I test for this whilst looping through an array.

Example:
for (var o in arr) {
if (!!arr[o].someProp) { // the someProp *is* always in the data object


That's one solution. If you have a way to recognize the proper data,
then just use that.

You can drop the "!!". All it does is to convert its operand to a
boolean, but it is being used inside a condition, so it will always
be converted to a boolean.
Would there be a better way to test for arr[o]=void (as per the Moz
debugger)?
I don't know the Moz debugger, but I'm pretty sure it's not "void".
More likely you mean:
arr[o]===undefined
The only thing I could come up with was the !!


Just plain
if (arr[o].someProp) { /// arr[0].someProp is not undefined

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5

P: n/a
Douglas Crockford wrote:
I've been running into something that strikes me as pretty annoying (esp
since I don't know how to work around it).
It concerns the extension of Object and the usage of Maps.

Following a snip I use to extend the Object object.

Object.prototype.extend = function (oSuper) {
for (sProperty in oSuper) {
this[sProperty] = oSuper[sProperty];
}
};

This all goes fine. The function available etc.
Here is the catch.

When I create ab arr=new Object() to be used as a Map or an arr=[] to be
used as a Map (as well) I can fill the arr. No problems so far.
When I use the for (o in arr) { ... } I keep on tripping over the the fact
that "extend" is a key with {ScriptFunction} as value.

Obviously this is *not* what I'm looking for.
Am I doing something wrong here and if so what?
Trying to rewrite my code every time this comes up is starting to ...

I consider that to be a design error in the language. The ECMAScript model
includes a property called DONTENUM which hides properties from the for..in
statement. Unfortunately, there is nothing in the language that allows you to
use it. That bites.

I have used two alternatives:

(1) Have a .bag member in your function object which holds the enumerated stuff.

(2) In your for..in loop, reject function values

for (n in o) {
if (typeof o[n] != 'function') {
...
}
}

Thx, I got it working by testing on a property I know exists in the object
being retrieved.
I do see the generic elegance though of your solution so I guess I'll do
some stepping through. ;)
http://www.ecmascript.org

Jul 20 '05 #6

P: n/a
Lasse Reichstein Nielsen wrote:
"F. Da Costa" <da*****@xs4all.nl> writes:

Just the verifiable content put in as data

I.e., Just the data you put there yourself, not the ones inherited from
the prototype.

User added properties cannot be made non-enumerable, so anything you
add will show up in a for(in)-loop.


This is fine, so I test for this whilst looping through an array.

Example:
for (var o in arr) {
if (!!arr[o].someProp) { // the someProp *is* always in the data object

That's one solution. If you have a way to recognize the proper data,
then just use that.

You can drop the "!!". All it does is to convert its operand to a
boolean, but it is being used inside a condition, so it will always
be converted to a boolean.

Ah, in the debugger I had to use the !! because it just looked at the
object normally and not from a condition p.o.v.
Would there be a better way to test for arr[o]=void (as per the Moz
debugger)?

I don't know the Moz debugger, but I'm pretty sure it's not "void".
More likely you mean:
arr[o]===undefined

I guess its a flavor thing. It did show void but the issue was as explained
above
The only thing I could come up with was the !!

Just plain
if (arr[o].someProp) { /// arr[0].someProp is not undefined

So it is.
Ths for the explanatory effort
/L

F DCG
Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.