What kind of data type is?

G

gtux

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??
 
W

web.dev

Hi everybody:

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.
 
Y

Yann-Erwan Perio

gtux said:
var fruit =
{
'apple' : { 'weight' : 10, 'cost' : 9},
'peach' : { 'weight' : 19, 'cost' : 10}
}

somebody can tell me what kind of data type is fruit??

'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.
 
V

VK

gtux said:
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??

Nested Hash table, aka Map table aka Associative array (choose what you
like).
 
R

Randy Webb

VK said the following on 7/19/2005 6:31 PM:
Nested Hash table, aka Map table aka Associative array (choose what you
like).

No, it is a plain Object and nothing more.
 
A

ASM

gtux said:
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}
}

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>
 
V

VK

var fruit =
No, it is a plain Object and nothing more.

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:
somebody can tell me what kind of data type is fruit?

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.
 
C

Christopher J. Hahn

VK said:
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:

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.


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.
 
R

Robert

VK said:
Nested Hash table, aka Map table aka Associative array (choose what you
like).

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?
 
V

VK

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.

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).

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.

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.
 
V

VK

Gegel's "all-containing something that became everything"

Gegel's "all-containing NOTHING that became EVERYTHING" (to not be
cought on wrong quoting).
 
L

Lasse Reichstein Nielsen

Robert said:
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?

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
 
C

Christopher J. Hahn

Didn't see this 'til just now.
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.

Prove it.
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.

HTMLCollection? No... just an object. And not even an ugly one-- I find
the object literal syntax quite pleasing to the eye.
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).

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.
Sure. And there is not Mr. Christopher J. Hahn, there is an
instantiated human object.

Utterly irrelevant, besides contradicting your preference for a
"lower-level" description of a high-level object.


[snipped silliness]
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.)

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).
 

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
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top