What are objects for


#1

what is wrong with my code?
var spencer = {
age: 22,
country: "United States"
};
var spencer2 = new Object ();
spencer2.country = "United States";
spencer2.age = 22;


#2

This SERP is one way to jump right into the definition of an object in programming.

Wikipedia summarizes it as,

"object" refers to a particular instance of a class where the object can be a combination of variables, functions, and data structures. In relational database management, an object can be a table or column, or an association between data and a database entity.

In JavaScript, we can take both usages into account since both apply. Consider the spence object in the above code:

var spencer = {
  age: 22,
  country: "United States"
};

In the key-value pairs, we are defining an association between a key name and associated data (which is also an object). This is in the literal, which is where the term object literal comes from. That's the data side of things.

The number literal is an instance of the Number class, and the key names (which are strings) and the string data are instances of the String class. spencer, as we can see by its constructor, is an instance of the Object class.

You'll get in this in much greater detail as you progress. In simple terms, the answer to your question is, everything. Objects are for everything, and everything is an instance of, or inherits from the Object class. You may run across the phrase, "Everything in JavaScript is an object." This is more or less what the phrase is saying.

The actual error may be the white space after Object. Try writing it like,

var spencer2 = new Object();

#3

my code is still not running, it's giving me the error of " make sure to use the Object constructor"


#4

here's my code again
var spencer = {
age: 22,
country: "United States"
};
var spencer2 = new Object ( );
spencer2.country = "United States";
spencer2.age = 22;
// make spencer2 here with constructor notation


#5

It still has the white space. Sorry I mentioned that in my edit, rather than the first posting. Change,

Object ()

to

Object()

Try that.