What data structure is this? - javascript

I came across a piece of code in JS

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

Using the variable declared above:

 alert(globe.country); 

Questions:

  • Is this a JS class with two members?
  • Why is the var keyword not used when declaring a globe?
  • If it is a class, can I have member functions?

thanks

+4
source share
3 answers
  • This is a JS object with two properties.

  • Not using var puts the variable in the global scope

  • Although it is not a class, it can still have functions as properties

Functions can be bound in two different ways:

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

or

 globe = { ... myFunc: function() { /* do something */ } } 
+11
source

This is a JavaScript object. Written in the text literature of the object.

+3
source

JavaScript is not an object-oriented language, so there are no classes in the same sense as in Java or C #. JavaScript is a language-based prototype . So this is an object with two members. You can add additional members, like you, to any other object, and they can be functions.

+1
source

All Articles