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

how to get an Object's properties' name?

P: n/a
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.
Nov 20 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
VK
On Nov 20, 8:47 pm, Max <Maximus.Zh...@gmail.comwrote:
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);
}
Nov 20 '07 #2

P: n/a
Max
On 11月21日, 上午2时15分, VK <schools_r...@yahoo.comwrote:
On Nov 20, 8:47 pm, Max <Maximus.Zh...@gmail.comwrote:
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);

}
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.
Nov 21 '07 #3

P: n/a
Max wrote:
On 11鏈21鏃, 涓婂崍2鏃15鍒, VK <schools_r...@yahoo.comwrote:
>On Nov 20, 8:47 pm, Max <Maximus.Zh...@gmail.comwrote:
>>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);

}

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
Nov 23 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.