Introducing Objects

Modern JavaScript Using Objects
5 minutes
Share the link to this page
Copied
  Completed

Transcript

If someone were to say to you that JavaScript is not an object oriented language as a means to dismiss what you're currently learning, they would be wrong. JavaScript is indeed an object oriented language. The difference is, is it does not use classes to define its object as most object oriented languages do. And so someone coming from that perspective might perceive JavaScript as something different. It is different, but it is still an object oriented language. In fact, objects are the building blocks of JavaScript.

You've already been using numerous objects whenever you create an array or whenever you create a function, you are dealing with objects, the date object, the math object, you are dealing with objects. So you have already used lots of objects. Now the one thing we have not dealt with is how to create User Defined objects. We've created function objects and array objects, but we have not created user defined objects within JavaScript yet. Now before we jump into that, I would like to review the concepts that we talked about when we introduced objects in the fundamentals section. These concepts will be a review, you have already seen this information, but based upon what additional information you have learned, I think there's some things to be gained from it.

So let's deal with that introduction. First, objects are simply a collection of values. Those values could be primitive data types, like we discussed already, those primitive data types in an object are called properties. They can be a function, which a function tied to an object is called a method. It could be arrays, or they could be other types of objects, objects, so an object can contain a lot of different types of other values. So we have here a sample object, we are referencing this object using the name user one.

So that's how we're going to reference this object. Just as with primitive data types that are assigned to variables, you have to have some way to reference them. That's true for objects as well. For this example, this object is going to keep data about a user. And so all the data attached to that object is going to be about a user. And so this happens to be user one.

So let's look at some of the data we could have associated with this object. We could have a user name. Now since an object is a collection of values, we need a way to refer to those values. So each value is given a name. In this case, the name of the value is username, the value is john. We call those collection of values.

In an object name value pairs because they consist of both the name and the value, they also can be referred as key value pairs. In fact, those two terms are used interchangeably. So the first data attached to this sample object we're looking at is the username. The value is john. The next name value pair is age the value is 32. Address, the value is 100.

South Main city, the value of Salt Lake City. So far, this object has four name value pairs. Now let's look at a name value pair that contains different information in this case, a function. So the name is full address, the contents or the value associated with that name is a function definition. So we could execute that function by Referring to the object and then referring to the name associated with that function. One more example of data associated with this object.

Let's say we're keeping track of comments made by this particular user. Well, in this case, we could use an array. So an array could be be associated with an object as well. And once again, it's stored as a name value pair. So the value doesn't have to simply be a primitive like we had with the name value pairs at the top of the object that can be more than that it could be a function, it could be array could be another object even. Now in JavaScript, when we want to refer to a value, that's a part of one of these name value pairs, we need to reference the name of the value.

And we do that using dot notation. There's actually a couple ways to do that. But right now we'll just introduce dot notation and then later in the course We'll talk about another method for doing this. The way dot notation works is it's done by referencing the object first. And then the name of the name value pair. So if we wanted to extract the username from this particular object, we would do user one dot user name.

To show this sample, in a JavaScript statement, we can use console dot log. So it'd be console dot log user, one dot username, and that would display the username. That's the basic way use dot notation to access the name value pairs that are part of an object. Let's move on and learn more about objects.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.