Link to home
Start Free TrialLog in
Avatar of zumpoof
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
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

I use a function

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
ASKER CERTIFIED SOLUTION
Avatar of anthonywjones66
anthonywjones66

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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].model)

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.

Avatar of anthonywjones66
anthonywjones66


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.
What are you missing?
Seems all is missing is a toString

http://www.crockford.com/JSON/js.html
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());
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.