What kind of data type is? | | |
Hi everybody:
I'm new in Javascript, I found some code and there is this:
var fruit =
{
'apple' : { 'weight' : 10, 'cost' : 9},
'peach' : { 'weight' : 19, 'cost' : 10}
}
somebody can tell me what kind of data type is fruit?? | | | | re: What kind of data type is?
>>Hi everybody:
[color=blue][color=green]
>> I'm new in Javascript, I found some code and there is this:[/color][/color]
[color=blue][color=green]
>>var fruit =
>>{
>> 'apple' : { 'weight' : 10, 'cost' : 9},
>> 'peach' : { 'weight' : 19, 'cost' : 10}
>>}
>>somebody can tell me what kind of data type is fruit??[/color][/color]
Hi gtux,
fruit is of type Object. More specifically, this is an object literal.
It's just another way of creating and initialize new objects.
The code that you see is creating a new Object with the properties
'apple', and 'peach'. In turn, 'apple' and 'peach, also have properties
'weight' and 'cost'.
Hope this clarifies. | | | | re: What kind of data type is?
gtux wrote:
[color=blue]
> var fruit =
> {
> 'apple' : { 'weight' : 10, 'cost' : 9},
> 'peach' : { 'weight' : 19, 'cost' : 10}
> }
>
> somebody can tell me what kind of data type is fruit??[/color]
'fruit' is an object with two properties 'apple' and 'peach', themselves
objects with two properties each ('weight' and 'cost').
This construct "{/*...*/}" is called object literal (or object
initialiser), i.e an expression which creates an object and initialises
it at the same time. It is equivalent to:
var fruit=new Object(); // or var fruit={};
fruit.apple= { 'weight' : 10, 'cost' : 9};
fruit.peach= { 'weight' : 19, 'cost' : 10};
or
var fruit=new Object(); // or var fruit={};
fruit.apple=new Object(); // or fruit.apple={};
fruit.apple.weight=10;
fruit.apple.cost=9;
//etc.
You can read more about the object literal in the ECMAScript
specification, §11.1.5.
HTH,
Yep. | | | | re: What kind of data type is?
gtux wrote:[color=blue]
> Hi everybody:
>
> I'm new in Javascript, I found some code and there is this:
>
> var fruit =
> {
> 'apple' : { 'weight' : 10, 'cost' : 9},
> 'peach' : { 'weight' : 19, 'cost' : 10}
> }
>
> somebody can tell me what kind of data type is fruit??[/color]
Nested Hash table, aka Map table aka Associative array (choose what you
like). | | | | re: What kind of data type is?
VK said the following on 7/19/2005 6:31 PM:[color=blue]
>
> gtux wrote:
>[color=green]
>>Hi everybody:
>>
>> I'm new in Javascript, I found some code and there is this:
>>
>>var fruit =
>>{
>> 'apple' : { 'weight' : 10, 'cost' : 9},
>> 'peach' : { 'weight' : 19, 'cost' : 10}
>>}
>>
>>somebody can tell me what kind of data type is fruit??[/color]
>
>
> Nested Hash table, aka Map table aka Associative array (choose what you
> like).[/color]
No, it is a plain Object and nothing more.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly | | | | re: What kind of data type is?
Thanks a lot for yours respons | | | | re: What kind of data type is?
gtux wrote:[color=blue]
> Hi everybody:
>
> I'm new in Javascript, I found some code and there is this:
>
> var fruit =
> {
> 'apple' : { 'weight' : 10, 'cost' : 9},
> 'peach' : { 'weight' : 19, 'cost' : 10}
> }[/color]
it is an object that could be assimilate to an array
(of fruits with a name, weight and cost)
copy+paste in a text editor the following
save in *.htm and open the file in a navigator :
<html><script type="text/javascript">
var fruit =
{
'apple' : { 'weight' : 10, 'cost' : 9},
'peach' : { 'weight' : 19, 'cost' : 10}
}
// one of traditionnal ways to set an array and sub-arrays
var vegetables = new Array('potatoes','beans');
vegetables['potatoes'] = new Array('weight','cost');
vegetables['potatoes']['weight'] = 10;
vegetables['potatoes']['cost'] = 9;
vegetables['beans'] = new Array('weight','cost');
vegetables['beans']['weight'] = 19;
vegetables['beans']['cost'] = 10;
// results and exploitation :
document.write('<p>apple : weight ='+fruit['apple']['weight']+
' - cost = '+fruit['apple']['cost'])
document.write('<p>vegetables : weight =' +
vegetables['potatoes']['weight'] +
' - cost = '+vegetables['potatoes']['cost'])
var content='';
for(var foo in fruit) {
content += '<p>'+ foo + ' = ';
for(var truc in fruit[foo]) {
content += truc + ' : ' + fruit[foo][truc] + ' | ';
}
}
document.write(content);
</script></html>
--
Stephane Moriaux et son [moins] vieux Mac | | | | re: What kind of data type is?
> >>var fruit =[color=blue][color=green][color=darkred]
> >>{
> >> 'apple' : { 'weight' : 10, 'cost' : 9},
> >> 'peach' : { 'weight' : 19, 'cost' : 10}
> >>}
> >>
> >>somebody can tell me what kind of data type is fruit??[/color]
> >
> >
> > Nested Hash table, aka Map table aka Associative array (choose what you
> > like).[/color]
>
> No, it is a plain Object and nothing more.[/color]
var objectObject = new Object();
This is the "plain Object" with only constructor and prototype in it.
You must meant to say that typeof(fruit) == 'object'. That's true but
it doesn't answer the OP question I quess:[color=blue]
> somebody can tell me what kind of data type is fruit?[/color]
Other words: how to call this particular data structure, how to refer
its internal data, using what methods.
This is a nested Hash table.
<http://www.geocities.com/schools_ring/ArrayAndHash.html#Hash_definition>
....
function fruit(fName,fWeight,fCost) {
this.name = fName;
this.weight = fWeight;
this.cost = fCost;
}
var apple = new fruit('apple',10,9);
....
Something like above would be more casual instead of this "hash twist",
but it may appear more code effective if you create dozens and hundreds
of records at once. | | | | re: What kind of data type is?
VK wrote:[color=blue][color=green][color=darkred]
> > >>var fruit =
> > >>{
> > >> 'apple' : { 'weight' : 10, 'cost' : 9},
> > >> 'peach' : { 'weight' : 19, 'cost' : 10}
> > >>}
> > >>
> > >>somebody can tell me what kind of data type is fruit??
> > >
> > >
> > > Nested Hash table, aka Map table aka Associative array (choose what you
> > > like).[/color]
> >
> > No, it is a plain Object and nothing more.[/color]
>
> var objectObject = new Object();
>
> This is the "plain Object" with only constructor and prototype in it.
>
> You must meant to say that typeof(fruit) == 'object'. That's true but
> it doesn't answer the OP question I quess:[color=green]
> > somebody can tell me what kind of data type is fruit?[/color]
>
> Other words: how to call this particular data structure, how to refer
> its internal data, using what methods.
>
> This is a nested Hash table.
> <http://www.geocities.com/schools_ring/ArrayAndHash.html#Hash_definition>
>
> ...
> function fruit(fName,fWeight,fCost) {
> this.name = fName;
> this.weight = fWeight;
> this.cost = fCost;
> }
>
> var apple = new fruit('apple',10,9);
> ...
>
> Something like above would be more casual instead of this "hash twist",
> but it may appear more code effective if you create dozens and hundreds
> of records at once.[/color]
What you're saying is that it's only an object because just about
everything's an object and since it looks like a hash, you can be more
specific and call it a hash.
I might agree if this 'hash' object had some functionality in line with
other hash implementations, but exceeded the base functionality of any
run-of-the-mill Object. But it doesn't.
The construct:
{ }
.... creates an object.
Fruit.prototype = {
weight: 0,
cost: 0
}
Is the prototype for a Fruit object a hash? Doesn't that mean that with
(x = new Fruit()), x is also a hash? But if that's true, aren't *all*
objects hashes?
No. Of course not.
They are objects. Objects that are so useful that you can use them much
like you would a hash. But you also use them like arrays. Or strings.
Or integers.
That does not make them hashes. Or arrays. Or strings. Or integers.
Objects are objects. | | | | re: What kind of data type is?
VK wrote:[color=blue]
>
> gtux wrote:
>[color=green]
>>var fruit =
>>{
>> 'apple' : { 'weight' : 10, 'cost' : 9},
>> 'peach' : { 'weight' : 19, 'cost' : 10}
>>}
>>[/color]
> Nested Hash table, aka Map table aka Associative array (choose what you
> like).
>[/color]
Does that mean that Javascript calculates the hash value of a property
whenever you add it to an object?
And isn't each object a hashtable in that case? | | | | re: What kind of data type is?
> What you're saying is that it's only an object because just about[color=blue]
> everything's an object and since it looks like a hash, you can be more
> specific and call it a hash.
>
> I might agree if this 'hash' object had some functionality in line with
> other hash implementations, but exceeded the base functionality of any
> run-of-the-mill Object. But it doesn't.
>
> The construct:
> { }
>
> ... creates an object.
>
> Fruit.prototype = {
> weight: 0,
> cost: 0
> }
>
> Is the prototype for a Fruit object a hash? Doesn't that mean that with
> (x = new Fruit()), x is also a hash? But if that's true, aren't *all*
> objects hashes?
>
> No. Of course not.[/color]
Of course not. But all objects based on the Object() implement hash
(associative array) data structure, because it appeared to be the most
convenient to keep Identifier : Property/Method pairs.
So yes, if we *really* want to, we can call the OP's code an ugly
object. If it would be the question of life or death to further
identify it, we could say that it's a bastard of HTMLCollection.
Without "life or death" issue I would find some lower level data
construct to describe it. And hash (associative array) is the best for
it. OP's code is still not a fully-qualified object, because it's
missing the most important part: internal methods to react on the world
and on what the world does with it (its data).
[color=blue]
> They are objects. Objects that are so useful that you can use them much
> like you would a hash. But you also use them like arrays. Or strings.
> Or integers.
>
> That does not make them hashes. Or arrays. Or strings. Or integers.
>
> Objects are objects.[/color]
Sure. And there is not Mr. Christopher J. Hahn, there is an
instantiated human object.
I just amazed. Is it some global trend in CS? When I worked with UC
schools in 90's, the philosophy in CS departments was at the very
bottom of the matter. An average CS student just needed to spell
Aristotle w/o mistake and to know that it was a very smart guy who
lived very long time ago in Europe.
And here I'm really enjoying the most profound developments and
implementations of the eidos theory by Aristotle and by Plato, Gegel's
"all-containing something that became everything", sensualisme by Jung
etc. etc.
Doesn't help to solve little programming proglems raised by Array vs
Hash vs HTMLCollection differences, for this you need to read
<http://www.geocities.com/schools_ring/ArrayAndHash.html>
(BTW the 3rd edition is coming soon.)
But for a conversation it's really enjoyable. | | | | re: What kind of data type is?
> Gegel's "all-containing something that became everything"
Gegel's "all-containing NOTHING that became EVERYTHING" (to not be
cought on wrong quoting). | | | | re: What kind of data type is?
Robert <robert@noreply.x> writes:
[color=blue]
> Does that mean that Javascript calculates the hash value of a property
> whenever you add it to an object?
> And isn't each object a hashtable in that case?[/color]
Whether the implementation behind objects uses hash tables is not
something the language specifies. No doubt some implementations do.
Each object allows dynamically updating its properties.
The notation:
var o = {foo:42}
is object literal notation. It creates objects, just as
var o = new Object();
o.foo = 42;
does, only shorter.
/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.' | | | | re: What kind of data type is?
Didn't see this 'til just now.
VK wrote:[color=blue][color=green]
> > The construct:
> > { }
> >
> > ... creates an object.
> >
> > Fruit.prototype = {
> > weight: 0,
> > cost: 0
> > }
> >
> > Is the prototype for a Fruit object a hash? Doesn't that mean that with
> > (x = new Fruit()), x is also a hash? But if that's true, aren't *all*
> > objects hashes?
> >
> > No. Of course not.[/color]
>
> Of course not. But all objects based on the Object() implement hash
> (associative array) data structure, because it appeared to be the most
> convenient to keep Identifier : Property/Method pairs.[/color]
Prove it.
[color=blue]
> So yes, if we *really* want to, we can call the OP's code an ugly
> object. If it would be the question of life or death to further
> identify it, we could say that it's a bastard of HTMLCollection.[/color]
HTMLCollection? No... just an object. And not even an ugly one-- I find
the object literal syntax quite pleasing to the eye.
[color=blue]
> Without "life or death" issue I would find some lower level data
> construct to describe it. And hash (associative array) is the best for
> it. OP's code is still not a fully-qualified object, because it's
> missing the most important part: internal methods to react on the world
> and on what the world does with it (its data).[/color]
An object doesn't need methods to be an object. An object is an object.
Aside from that, if you're saying that since it has no methods it is
not an object but a hash, try this:
alert( {}.toString );
It's somewhat pointless to try to talk about low-level data structures
in the context of JavaScript, since we have no access to them except by
interface.
[color=blue][color=green]
> > They are objects. Objects that are so useful that you can use them much
> > like you would a hash. But you also use them like arrays. Or strings.
> > Or integers.
> >
> > That does not make them hashes. Or arrays. Or strings. Or integers.
> >
> > Objects are objects.[/color]
>
> Sure. And there is not Mr. Christopher J. Hahn, there is an
> instantiated human object.[/color]
Utterly irrelevant, besides contradicting your preference for a
"lower-level" description of a high-level object.
[snipped silliness]
[color=blue]
> Doesn't help to solve little programming proglems raised by Array vs
> Hash vs HTMLCollection differences, for this you need to read
> <http://www.geocities.com/schools_ring/ArrayAndHash.html>
> (BTW the 3rd edition is coming soon.)[/color]
So far as I'm aware, there are no problems to solve, so no URL is
needed.
An Array is a type of Object. There is no Hash in JavaScript.
Problems only arise when you try to think of these higher-level
concepts (Objects) in lower-level terms (i.e. Hash). |  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,419 network members.
|