zumpoof
asked on
Complex Javascript variable example using colons for hashes. Nested hashes, arrays,etc
Could someone give me an example of a complex js variable using ':' for hash declarations? I recall hearing this could be done. Here's an example in perl for what I'd want in JS.
$me = {
'cars' => [
{'make' => 'ford',
'model' => 'Mustang},
{'make' => 'Ford',
'model' => 'Escourt'}
'computers'
....
Thanks,
Zumpoof
$me = {
'cars' => [
{'make' => 'ford',
'model' => 'Mustang},
{'make' => 'Ford',
'model' => 'Escourt'}
'computers'
....
Thanks,
Zumpoof
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks Anthony. I was looking for that too yesterday
me = {
cars: [
{make: 'ford', model: 'Mustang'},
{make: 'Ford', model: 'Escort'}
],
computers: [
{make: 'dell', model: 'dimension'}
]
}
document.write("me")
for (o in me)
for (p in me[o]) document.write('<br>'+ o+':'+me[o][p].make+'/'+me [o][p].mod el)
Points to Anthony
me = {
cars: [
{make: 'ford', model: 'Mustang'},
{make: 'Ford', model: 'Escort'}
],
computers: [
{make: 'dell', model: 'dimension'}
]
}
document.write("me")
for (o in me)
for (p in me[o]) document.write('<br>'+ o+':'+me[o][p].make+'/'+me
Points to Anthony
There is a site that Is pushing for that use of this syntax
(JavaScript Object Notation)
http://json.org
In other languages as well.
I've considered JSON in past but as yet I haven't found a way to use it to populate an object that has both data and function. It would be nice to have a set of prefined 'classes' (functions with extended prototypes) that can be instanced and populated with JSON or JSON like syntax. As yet I've not been able to solve that one. It is a very appealling alternative to XML.
Anthony.
anthony:
function mult(){
return this.data1*this.data2
}
var thisobject = { data1:1,
data2:2,
f1:function(){return this.data1+this.data2} ,
f2:mult
}
alert(thisobject.f1());
alert(thisobject.f2());
thisobject.data1=3;
alert(thisobject.f1());
alert(thisobject.f2());
function mult(){
return this.data1*this.data2
}
var thisobject = { data1:1,
data2:2,
f1:function(){return this.data1+this.data2} ,
f2:mult
}
alert(thisobject.f1());
alert(thisobject.f2());
thisobject.data1=3;
alert(thisobject.f1());
alert(thisobject.f2());
Chaps,
I don't want to take up too much of your time or hijack this question for one of my own. When I get the chance I'll formulate description of what I would've liked to do with JSON as a base then perhaps one of you clever boffins might see a solution and earn some points in the process. :)
Anthony.
I don't want to take up too much of your time or hijack this question for one of my own. When I get the chance I'll formulate description of what I would've liked to do with JSON as a base then perhaps one of you clever boffins might see a solution and earn some points in the process. :)
Anthony.
function car(model,make) {
this.model=model
this.make=make
}
cars = new Array(
new car("ford","mustang"),
new car("ford","escort")
)
But you are right and I cannot remember the syntax which does resemble the perl one