Object.keys

32 views
Skip to first unread message

Lee Goddard

unread,
Oct 10, 2012, 8:50:45 AM10/10/12
to mootool...@googlegroups.com
I'm sure I'm doing something daft, but
why doesn't Object.keys(this.obj) work?

http://jsfiddle.net/leegee/cvrvK/4/

Thanks...

Lee

Oliv

unread,
Oct 10, 2012, 10:44:12 AM10/10/12
to mootool...@googlegroups.com, l...@leegoddard.net
Hi,

In the Object.keys function, it is using the hasOwnProperty function as a test before returning keys, and it will return false if the key comes from an inherited (ie prototype) property.

During the mootools Class object creation, the object passed in parameter is merged in the prototype of the object Class, so you can't get the keys with Object.keys().


I hope to be understandable, cheers, Olivier Gasc

Lee Goddard

unread,
Oct 10, 2012, 10:52:18 AM10/10/12
to Oliv, mootool...@googlegroups.com
Thanks, Olivier,

I understand, though I'm not happy about what's going on!

Lee
> <http://jsfiddle.net/leegee/cvrvK/4/>
>
> Thanks...
>
> Lee
>


Dimitar Christoff

unread,
Oct 10, 2012, 1:33:48 PM10/10/12
to mootool...@googlegroups.com


On Wednesday, October 10, 2012, Lee Goddard wrote:
Thanks, Olivier,

I understand, though I'm not happy about what's going on!

Lee


this is because elements is on the prototype. you have 2 patterns here. 

one. Object.keys(Object.clone(this.elements))

two. don't add this to the proto but instance. eg

addElements: function(){
    this.elements = {}
},
initialize: function(){
    this.addElements();
    Object.keys(this.elements);
}

something like that
--
Dimitar Christoff

"JavaScript is to JAVA what hamster is to ham"
@D_mitar - https://github.com/DimitarChristoff

Lee

unread,
Oct 10, 2012, 2:41:35 PM10/10/12
to mootool...@googlegroups.com

On 10/10/2012 19:33, Dimitar Christoff wrote:
>
>
> On Wednesday, October 10, 2012, Lee Goddard wrote:
>
> Thanks, Olivier,
>
> I understand, though I'm not happy about what's going on!
>
> Lee
>
> this is because elements is on the prototype. you have 2 patterns
> here.
>
> one. Object.keys(Object.clone(this.elements))
>
> two. don't add this to the proto but instance

Yes, thanks Dimitar. I realise that. I'm probably the only one who'd
want to know what fields my object has, so I'll not suggest a patch!

Cheers
Lee



Sanford Whiteman

unread,
Oct 10, 2012, 3:35:37 PM10/10/12
to Lee
> Yes, thanks Dimitar. I realise that. I'm probably the only one who'd
> want to know what fields my object has, so I'll not suggest a patch!

It's not *that* weird of a need IMO... you could write
Object.Keys which would basically be the diff of Object.keys and
a full in-loop.

-- S.


Sanford Whiteman

unread,
Oct 10, 2012, 5:19:30 PM10/10/12
to Sanford Whiteman
> It's not *that* weird of a need IMO... you could write
> Object.Keys which would basically be the diff of Object.keys and
> a full in-loop.

Sorry, editing typo, I meant ^H^H^H Object.otherKeys or suchlike.

-- S.

Reply all
Reply to author
Forward
0 new messages