first object property

A

-a

i'm getting an ajax/json response back which is a hash/object with
exactly one key. they key's value determines the kind of record
returned. i wanted a concise way to grab the *first* key from an
object and this is the best i came up with

Object.prototype.first_property = function(){

var property;

for( property in this){ break; }

return property;
}

( { a : 42, b : 42 } ).first_property(); // => 'a'

is there a simpler method of doing this?

kind regards.
 
T

Thomas 'PointedEars' Lahn

-a said:
i'm getting an ajax/json response back which is a hash/object with
exactly one key. they key's value determines the kind of record
returned. i wanted a concise way to grab the *first* key from an
object and this is the best i came up with

Object.prototype.first_property = function(){

Augmenting the Object prototype object is not a good idea, unless you
provide means so that the for...in iteration and `in' operation are not
compromised by your adding an enumerable property.
var property;

for( property in this){ break; }

return property;

Given that `this' inherits from Object.prototype through the prototype
chain, it is possible that you returned "first_property" here. See below.
}

( { a : 42, b : 42 } ).first_property(); // => 'a'

is there a simpler method of doing this?

There is *no* method of doing this because the specification says that the
iteration order for the for...in statement is arbitrary, and implementations
have showed to work as specified in that regard.

IOW: There is no first property.


PointedEars
 
M

Martin Honnen

-a said:
i wanted a concise way to grab the *first* key from an
object and this is the best i came up with

Object.prototype.first_property = function(){

var property;

for( property in this){ break; }

return property;
}

( { a : 42, b : 42 } ).first_property(); // => 'a'

is there a simpler method of doing this?

There is no such thing as the "first property" defined. That code could
return any of the properties the object has, including the
first_property method itself. A Javascript object is an _unordered_
collection of properties.
 
T

Thomas 'PointedEars' Lahn

Martin said:
There is no such thing as the "first property" defined. That code could
return any of the properties the object has, including the
first_property method itself.

Not the method, only the name of the property that has been assigned a
reference to it. If it should be the method itself, that would have had
to be

return this[property];
A Javascript object is an _unordered_ collection of properties.

I think that statement is too restrictive. Of course the for...in order is
arbitrary, but there are instances where some element of order comes into
play; say with Array objects having stringified numbers as property names,
and DOM collection objects where the constraint of order is even more
strict. (That is not to say that for...in works in the numerical order there.)


PointedEars
 
A

-a

There is *no* method of doing this because the specification says that the
iteration order for the for...in statement is arbitrary, and implementations
have showed to work as specified in that regard.
IOW: There is no first property.

??

when there is only *one* property, as in this object

{ 'city' : ... data ... }

surely we can assume an iteration order!?

wrt to your 'this' comment that impl is indeed flawed, my current impl
passes the object in to the method

var first_property = function(obj){
var prop;
for(prop in obj){ break }
return prop;
}

so i ask again - for an object with *one* property, what is the
cleanest way to retrieve said property's name?

kind regards.
 
A

-a

There is no such thing as the "first property" defined. That code could
return any of the properties the object has, including the
first_property method itself. A Javascript object is an _unordered_
collection of properties.

is it not fair to assume this object has one property and that the
order is known apriori?

{ 'a' : 42 }

??


are there cases where the above object will have more than one
propery?


- kind regards
 
G

Gregor Kofler

-a meinte:
is it not fair to assume this object has one property and that the
order is known apriori?

{ 'a' : 42 }

??


are there cases where the above object will have more than one
propery?

Perhaps all the properties assigned to the Object.prototype? To my
understanding, Thomas and Martin already pointed that out.

Gregor
 
T

Thomas 'PointedEars' Lahn

Please provide proper attribution. That includes at least the name of the
person whose statement you are referring to.

BTW: Do you have a name?

Yes, there isn't.
when there is only *one* property, as in this object

{ 'city' : ... data ... }

surely we can assume an iteration order!?

That object may inherit an enumerable property from Object.prototype, so no.
wrt to your 'this' comment that impl is indeed flawed, my current impl

cn w 1337 wrds 2
passes the object in to the method

var first_property = function(obj){
var prop;
for(prop in obj){ break }
return prop;
}

`this' is not the flaw in your implementation. Considering Murphy's Law,
`obj' will be a reference to an object that inherits from Object.prototype
as well.
so i ask again - for an object with *one* property, what is the
cleanest way to retrieve said property's name?

Yours.

But since all native objects inherit from Object.prototype through the
prototype chain, and all host-defined objects I have encountered so far
have more than one property, I think it is safe to say that there is most
certainly no object that has only one property.

And since Object.prototype may have host-defined properties that do not
need to be enumerable or otherwise implement the property retrieval methods
defined in ECMAScript Edition 3, you cannot know whether an object has only
one *enumerable* property in the first place.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
-a said:
passes the object in to the method

var first_property = function(obj){
var prop;
for(prop in obj){ break }
return prop;
}
[...]

so i ask again - for an object with *one* property, what is the
cleanest way to retrieve said property's name?

Yours.

Correction: That one property can be non-enumerable, and so would not show
up in that loop.


PointedEars
 
M

Martin Honnen

Thomas said:
Martin Honnen wrote:

I think that statement is too restrictive.

Well section 8.6 of ECMAScript edition 3 says "An Object is an unordered
collection of properties."
 
T

Thomas 'PointedEars' Lahn

Martin said:
Well section 8.6 of ECMAScript edition 3 says "An Object is an unordered
collection of properties."

I know. I was talking practice, not theory :) Never mind.


Regards,
PointedEars
 
A

-a

Please provide proper attribution. That includes at least the name of the
person whose statement you are referring to.


BTW: Do you have a name?

From: (e-mail address removed)

http://www.google.com/search?q=ara....s=org.mozilla:en-US:official&client=firefox-a

Yes, there isn't.




That object may inherit an enumerable property from Object.prototype, so no.

seems like hasOwnProperty can handle this?

cn w 1337 wrds 2



`this' is not the flaw in your implementation. Considering Murphy's Law,
`obj' will be a reference to an object that inherits from Object.prototype
as well.


Yours.

But since all native objects inherit from Object.prototype through the
prototype chain, and all host-defined objects I have encountered so far
have more than one property, I think it is safe to say that there is most
certainly no object that has only one property.

And since Object.prototype may have host-defined properties that do not
need to be enumerable or otherwise implement the property retrieval methods
defined in ECMAScript Edition 3, you cannot know whether an object has only
one *enumerable* property in the first place.


i think all i need to worry about are methods/properties added to
Object.prototype: as i mentioned in the original posting i *do* know
that this particular object has only one property because it's a json
server response that i'm generating.

this seems to be working pretty well for me at the moment in ff, ie,
and safari

<html>
<head>
<script>

Object.prototype.own_properties = function(callback){
var props = [];
for(var prop in this){
if(this.hasOwnProperty(prop)){
props.push(prop)
try{
callback && callback(prop);
}
catch(e){
if(e['return']){ return e['return'] }
else{ throw e }
}
}
}
return props;
}

Object.prototype.first_property = function(){
return this.own_properties(function(prop){
throw({ 'return' : prop }) }
);
}

Object.prototype.foo = 'foo';
Object.prototype.bar = function(){ return 'bar' };

var obj = { 'key' : 'value' };

alert('obj.own_properties(): [' + obj.own_properties().join(',
') + ']');
alert('obj.first_property(): ' + obj.first_property());

</script>
</head>
<body></body>
</html>


anyone see major holes in it?


kind regards.
 
T

Thomas 'PointedEars' Lahn

-a wrote:
^^^^^^^^

That is not what was meant with proper attribution.
From: (e-mail address removed)

That is maybe an e-mail address, not a name.

Don't be ridiculous.
seems like hasOwnProperty can handle this?

Handle what? The properties of an object are not restricted to those where
Object.prototype.hasOwnProperty() returns true. (If you think about it,
this statement is self-evident.)

I was mistaken, see my other followup.
i think all i need to worry about are methods/properties added to
Object.prototype: as i mentioned in the original posting i *do* know
that this particular object has only one property

No, you don't, ...
because it's a json server response that i'm generating.

.... exactly *because* of that.

As I said, you have still not understood that native objects always inherit
from Object.prototype. So there cannot be a native object that has only one
property.

Even if we would assume that an object only can be considered to have a
property if that property was not inherited through the scope chain, that
property would still have to be enumerable or you would never spot it in
a for...in loop.
this seems to be working pretty well for me at the moment in ff, ie,
and safari [...]

It does not because it can not.

Please trim your quotes.


PointedEars
 
A

-a

this seems to be working pretty well for me at the moment in ff, ie,
and safari [...]

It does not because it can not.

did you run it?

i'm not really interested in theoretical disscusions as there are very
few things which can be claimed to 'always' be true in a open, closure
based language. however, if you can break this code in any
straightforward manner i'd be happy for you to demonstrate it:


<html>
<head>
<script>

Object.prototype.own_properties = function(callback){
var props = [];
for(var prop in this){
if(this.hasOwnProperty(prop)){
props.push(prop)
try{
callback && callback(prop);
}
catch(e){
if(e['return']){ return e['return'] }
else{ throw e }
}
}
}
return props;
}

Object.prototype.first_property = function(){
return this.own_properties(function(prop){
throw({ 'return' : prop }) }
);
}

Object.prototype.foo = 'foo';
Object.prototype.bar = function(){ return 'bar' };

var json_response_from_server = ' { foo : bar } ';
var obj = eval( '(' + json_response_from_server + ')' );

alert('obj.own_properties(): [' + obj.own_properties().join(',
') + ']');
alert('obj.first_property(): ' + obj.first_property());

</script>
</head>
<body></body>
</html>


again, i'm not interested in this code working for *all* objects, only
for objects returned from a server as defined above with a known
format of '{ type : data }'

regards.
 
T

Thomas 'PointedEars' Lahn

-a said:
did you run it?

I don't have to.
again, i'm not interested in this code working for *all* objects, only
for objects returned from a server as defined above with a known
format of '{ type : data }'

That would be '{name: data}', but what you don't appear to understand is
that this is not the object you are working on later. JSON returns object
literal syntax as a string that is then evaluated using eval() or another
parser. The resulting object, however, must be the same as if it would be
created using an Object initializer in the first place (you would not be
able to use for...in otherwise). So all that was said about objects does
apply to it, of course.

Your approach may work in some tested environments, however that does not
make it reliable as what counts are the possible peculiarities of the
execution environment that you *can* *not* know about. Given that, all bets
are off, unless you parse the returned string expression yourself, and
determine the first "property" (that is here, the first name-value pair)
from that string instead of from the object created (probably using Regular
Expressions.) So far, you are not doing that.


Your quotation style has improved, but you are still failing to provide
proper attribution. Since your common sense does not appear to serve,
please adhere to http://www.jibbering.com/faq/faq_notes/clj_posts.html


PointedEars
 
A

-a

That would be '{name: data}',

no. it's '{ type : data }'. name | type both are simply labels but
my code
specifically is using the first property to indicate the *type* of
response.
Your approach may work in some tested environments, however that does not
make it reliable as what counts are the possible peculiarities of the
execution environment that you *can* *not* know about. Given that, all bets
are off, unless you parse the returned string expression yourself, and
determine the first "property" (that is here, the first name-value pair)
from that string instead of from the object created (probably using Regular
Expressions.) So far, you are not doing that.

i understand all that perfectly well and it seems completely moot:
what you are
saying is that we cannot know apriori every execution environment a
peice of js
will run in. while that may be true we must make certain assumptions
in order
to get real work done regarding the most *common* execution
environments and
can only hope to write code which behaves properly in them. real
people, as it
turns out, don't care about standards or theory, only if code runs on
their own
machine. if we were to follow your logic we would not use
'array.length'
because, technically, we can not know whether all possible
implementations have
defined it as a property: it may be a method in some imagined
envrionment.

basically, if hasOwnProperty is implemented correctly then i think the
code
should work - otherwise it will not. my question, therefore, is
whether hasOwnProperty is portable enough across browsers and,
furthermore, if
anyone actually knows of a js runtime which does in fact add
properties to the
native object that are enumerable? if so then my code is definitely
flawed.
 
R

RobG

-a meinte:



Perhaps all the properties assigned to the Object.prototype? To my
understanding, Thomas and Martin already pointed that out.

More than just Object.prototype - any enumerable property on the
prototype chain, which may be longer than just that.

For the OP:
For..in is the only way (unless you are going to try random strings)
filtering of inherited properties can be done with hasOwnProperty.
 
A

-a

understanding, Thomas and Martin already pointed that out.

More than just Object.prototype - any enumerable property on the
prototype chain, which may be longer than just that.

For the OP:
For..in is the only way (unless you are going to try random strings)
filtering of inherited properties can be done with hasOwnProperty.


thanks. for the record the code i have is working well on all
required platforms.

ps.

sorry for the formatting on the last message, i'm bouncing between
computers today and google groups leaves something to be desired as a
usenet reader...

cheers.
 
D

Dr J R Stockton

In comp.lang.javascript message <nKGdnQ1p0Kvjk1_bnZ2dnUVZ_hninZ2d@comcas
Are you this much of a dick to everyone?

That's hard to say, without asking everyone or expecting an honest
answer from dick. But there seems to be no evidence to the contrary.

*Your* attribution, however, like dick's, contains less than recent
draft recommendations have considered adequate.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top