Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which datastructure is this? - Javascript

Tags:

javascript

I came across a code snippet in JS

globe =
{
   country : 'USA',
   continent : 'America'
}

Using the variable declared above by:

alert(globe.country);

Questions:

  1. Is this a JS class with 2 members?
  2. Why is the var keyword not used when declaring globe?
  3. If it's a class, can I have member functions as well?

Thanks

like image 888
Nick Avatar asked Dec 08 '22 06:12

Nick


2 Answers

  1. That is a JS object with two properties.

  2. Not using var places the variable in the global scope

  3. Though not a class, it can still have functions as properties

The functions can be tacked on two different ways:

globe.myFunc = function() { /* do something */ };

or

globe = {
    ...
    myFunc: function() { /* do something */ }
}
like image 175
geowa4 Avatar answered Dec 25 '22 06:12

geowa4


That is a JavaScript object. Written in the object literal notation.

like image 36
Lark Avatar answered Dec 25 '22 07:12

Lark