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?? 13 2278
>>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??
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.
gtux wrote: 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.
gtux wrote: 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).
VK said the following on 7/19/2005 6:31 PM: gtux wrote:
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).
No, it is a plain Object and nothing more.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Thanks a lot for yours respons
gtux wrote: 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>
--
Stephane Moriaux et son [moins] vieux Mac
> >>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).
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.
VK wrote: >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).
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.
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.
VK wrote: gtux wrote:
var fruit = { 'apple' : { 'weight' : 10, 'cost' : 9}, 'peach' : { 'weight' : 19, 'cost' : 10} } 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?
> 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.
> Gegel's "all-containing something that became everything"
Gegel's "all-containing NOTHING that became EVERYTHING" (to not be
cought on wrong quoting).
Robert <ro****@noreply.x> writes: 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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Didn't see this 'til just now.
VK wrote: 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.
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. 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.
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). This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: allyn44 |
last post by:
HI--what I am trying to do is 2 things:
1. Open a form in either data entry mode or edit mode depending on
what task the user is performing
2. Cancel events tied to fields on the form if I am in...
|
by: A.P. Hofstede |
last post by:
Could someone tell me where MS-Access (current and 97?) fit(s) on the
RDBMS - ORDBMS - ODBMS spectrum?
I gather it's relational, but how does it size up against/follow SQL2/3/4
definitions and how...
|
by: jacob navia |
last post by:
I would like to add at the beginning of the C tutorial I am writing
a short blurb about what "types" are. I came up with the following text.
Please can you comment?
Did I miss something?
Is...
|
by: serge calderara |
last post by:
Dear all,
Does this datalist control is somehow similar as a datasource for any other
control ?
I really to catch the use of it, can we assimilate that control as a kind of
dataset or...
|
by: Abhishek |
last post by:
why do I see that in most C programs, pointers in functions are
accepted as:
int func(int i,(void *)p) where p is a pointer or an address which is
passed from the place where it is called. what...
|
by: Xah Lee |
last post by:
in March, i posted a essay “What is Expressiveness in a Computer
Language”, archived at:
http://xahlee.org/perl-python/what_is_expresiveness.html
I was informed then that there is a academic...
|
by: Jeff |
last post by:
ASP.NET 2.0
This code crashes. It generate this error:
Value cannot be null.
Parameter name: type
I've created some custom web.config settings and this crash is related to
accessing theme...
|
by: jason |
last post by:
Hello,
I have a question about what kind of datastructure to use. I'm reading
collumn based data in the form of:
10\t12\t9\t11\n
24\t11\t4\t10\n
.....
I now have a structure which allows me...
|
by: klarae99 |
last post by:
Hello,
I am working in Access 2003 to create a database to record information about an annual fundraiser. I was hoping someone could review my table structure and make sure that it is normalized...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |