2

I just heard about JavaScript objects and was wondering what they are (because I cannot find any information out there) and what they are useful for.

I really just need help with that. Sorry I am a beginner.

6
  • Is this a question about JSON or about objects in general? Commented Sep 5, 2010 at 1:16
  • 1
    Could you be more specific? If you want the list of objects available in javascript - there you go: devx.com/projectcool/Article/19993 Commented Sep 5, 2010 at 1:18
  • @chromedude: seriously? did you try this? google.com/#hl=en&q=javascript+objects Commented Sep 5, 2010 at 1:28
  • @quixoto yes I did, what I mean is the type that you would put in arrays. Those results do not show what I want. Sorry, I am very new to javascript. Commented Sep 5, 2010 at 1:34
  • 2
    @chromedude: See Daniel's answer for an example. They can be anything at all. Sorry to have been harsh-- the statement "cannot find any information out there" about something so fundamental made it sound like you hadn't made any attempt to find out. Commented Sep 5, 2010 at 1:37

3 Answers 3

6

Apart from the few primitive types (numbers, strings, booleans, null and undefined) everything is an object in JavaScript (even functions).

Objects are basically containers of properties, which happen to be very useful for collecting and organizing data.

One popular method to create objects is to use the object literal notation:

var emptyObject = {};

var myFirstObject = {
   'name': 'Bobby',
   'surname': 'Smith'
};

The quotes around property names are optional if the name would be a legal JavaScript identifier and not a reserved word. A property's name can be any string. Objects can contain other objects, so they can easily represent trees or graphs:

var myFlight = {
   'airline': 'Airline Name',
   'number': 'AN700',
   'departure': {
      'IATA': 'SYD',
      'time': '2010-09-04 23:10:00'
   },
   'arrival': {
      'IATA': 'LAX',
      'time': '2010-09-05 05:14:00'
   }      
};

JavaScript objects also happen to be a convenient hash table data structure. You could easily do the following:

var myHashTable = {};
myHashTable['name'] = 'Bobby';
myHashTable['surname'] = 'Smith';
alert(myHashTable['name'] + ' ' + myHashTable['surname']);

This is definitely not an exhaustive answer, but I hope it gets you going in the right direction when doing further research.

Sign up to request clarification or add additional context in comments.

4 Comments

That is exactly what I am talking about. Is there particular thing that objects are good for (the type you show in your code) ?
@chromedude: Updated once again. Mentioned hash tables, which are very useful.
@Daniel Vassallo hmm... I guess I do not completely get the hash table example. What are hash tables?
@chromedude: They're very convenient (generally fast) data structures. Wikipedia has more info: en.wikipedia.org/wiki/Hash_table, but you'll also find more info online.
1

Well, the best source of information is the ECMASCript specification (of course) :)

In JavaScript there are 6 types.... 5 of them are primitive and the sixth type is object. Objects are all functions, all arrays, host objects (like the window object, the document object, every DOM node), built-in constructor objects (Date, Error, ...), other built-in objects (Math, JSON)...

When someone says JavaScript object, usually he means native objects which are defined in chapter 15. of the ECMAScript spec.

Comments

1

Short answer: it helps if you think in terms of "EVERYTHING is an object".

Longer answer:

Javascript has "data" (for example, your variable "john" and the values it contains) and "functions" (bits of Javascript code that act on data).

An "object" is a programming construct that combines "data" and "functionality" in one place. An "object" is a more powerful construct than either "data" or "functions" regarded separately.

For example, a "shape" object (an "object class") might know how to "draw" itself (an "object method"), regardless of whether it's a "square", a "circle" or a "triangle" (all "object instances").

An "object", as Daniel Vassallo pointed out above, can also be "a container of properties".

'Hope that helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.