how to get an Object's properties' name?

M

Max

Hi, there,

Is there a good way to get an Object's properties' names.
eg. obj={a:"aa",b:"bB",c:"Cc"};
All I mean is to get the names of "a", "b", "c". but not the values
"aa", "bB", "Cc".

Many thanx.
 
V

VK

Hi, there,

Is there a good way to get an Object's properties' names.
eg. obj={a:"aa",b:"bB",c:"Cc"};
All I mean is to get the names of "a", "b", "c". but not the values
"aa", "bB", "Cc".

var obj={a:"aa",b:"bB",c:"Cc"};

for (var prop in obj) {
alert(prop);
}
 
M

Max

var obj={a:"aa",b:"bB",c:"Cc"};

for (var prop in obj) {
alert(prop);

}

Thanks VK,

It is the way.
I just make a function with an object as the argument./* function ({})
{}*/ And the object has some properties that have default values.
and when the parameter do not have all the properties that defined the
default argument object, the missing properties would take the default
values.

thans again.
 
T

Thomas 'PointedEars' Lahn

Max said:
Thanks VK,

It is the way.
I just make a function with an object as the argument./* function ({})
{}*/ And the object has some properties that have default values.

This object should not have any enumerable properties, unless some fool
augmented the Object.prototype object.
and when the parameter do not have all the properties that defined the
default argument object, the missing properties would take the default
values.

The for..in operation will yield all properties of the object, whether
they are inherited or not.

The obvious solution is to use a prototype. That requires a user-defined
constructor:

function MyObject()
{
}

An associated prototype object:

MyObject.prototype = {
constructor: MyObject,
a: "defaultA",
b: "defaultB",
c: "defaultC"
};

And a constructor call:

var o = new MyObject();

If the properties of the constructed object are not set in the first place
or they are subject to a `delete' operation, the object will inherit the
properties from its prototype object through the prototype chain, and so the
default values.


PointedEars
 

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

No members online now.

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top