Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Working with objects

From: JavaScript Essential Training

Video: Working with objects

So JavaScript does have this idea of objects, although if you're used to formal object-oriented programming languages, you'll find it very basic. It has a few very straightforward ideas about what makes an object, and we've already been doing it. If we make an array, that's an object. If we make a date, that's an object. These objects are self-contained pieces. If I have an array, I can use the dot operator to say I'm interested in its .length. In this case it would be 5. If I have a date, I can call methods of it. Again, these are functions that belong to an object.

Working with objects

So JavaScript does have this idea of objects, although if you're used to formal object-oriented programming languages, you'll find it very basic. It has a few very straightforward ideas about what makes an object, and we've already been doing it. If we make an array, that's an object. If we make a date, that's an object. These objects are self-contained pieces. If I have an array, I can use the dot operator to say I'm interested in its .length. In this case it would be 5. If I have a date, I can call methods of it. Again, these are functions that belong to an object.

So I can say getTime or getFullYear, and as soon as we start touching our web pages, we'll have other objects that represent all the different pieces of the page, what's referred to as the DOM or Document Object Model, and we can even make our own. So we're going to get comfortable working with objects. And at its core, the idea of an object is really simple. It's just a container that gathers together some data and some behavior. So we know how to make variables. We know how to make functions.

An object just allows us to gather variables and functions that belong together and give them a name. So let's say here I'm making some regular variables: playerName, playerScore, playerRank. It's just creating those three in memory. And I realize that really these belong together. It would be nice if I could enclose them in a larger object. Now, I could do this in an array, but I don't really want to have to worry about the 0, 1, 2 whole idea. So what I'll do instead is I'll run this line.

First off, I'll say var player--again, I'm still creating a variable--equals new Object. This creates me a generic object container and calls it player. What I then do is just decide, what do I want the internal pieces of this object to be? So in this case, player.name = "Fred", player.score = 10000, player.rank = 1. So when we do this our data is now one level deeper. It's inside this player object.

So we use what's called dot syntax to say this object dot this variable. Although officially speaking, when they're outside an object we call them variablesm when they're inside an object we call them properties. A property is really just a variable that belongs to an object. Now, because it's quite useful to do this, the same way that there is shorthand for creating arrays, there is also a shorthand for creating an object. I create a new variable-- I'll call it player1-- and I use the curly braces. Now, here I'm not using them to surround a if statement code block or in a while loop.

This is a shorthand for creating an object. And then inside the curly braces I use this name value pair, in this case three sets of information: name: "Fred," score: 10000, rank: 1 and it creates the object and those properties inside it. And then I could just do another line of code and create player2, use the same format, but create a different object with different properties. And in this fashion I could get to player1.name, I could get to player2.name, player3.name, and so on.

So if we create a couple of objects, it's very easy to then write out information about each individual object. In this case, we'll write out console.log(player1.name). That reaches inside the object and writes out Fred. But it'd be nice to do a bit more to this. What I'd also like to do is associate some behavior with these objects, not just some data, not just some information, but what can player1 do? What can player2 do? What would be nice is if we could create a function called say playerDetails and that one I'll just put a comment inside this right now that this should display information about each player.

Now sure, what I could do is just define this function independently and declare it as accepting a parameter, but I'm not going to do it that way. What I'd like to do is make this function part of these two objects. And the way I need to do that is to associate it with the objects themselves. What I'm really doing here is making this a method of my objects. So what I'd like to be able to say is say player1.showDetails or player1.logDetails and have it call this function. I am associating this function with this object.

And it's completely up to me how I want to do this. I'm going to say player1.logDetails. You might think, well, where that this come from? I'm just making it up right now, and I'm setting it equal to playerDetails, which is the name of the function. I'm not calling it here. I'm not calling that function at all. I'm just associating it with my player1 object. And similarly, what I want to do is exactly the same thing with the player2 object. So what does this do? Well, now both of these objects actually have a method called logDetails, and if we call it, it will just reach through and call the playerDetails function.

Okay, so what. What will it do? Well, I'd like to actually, let's just do a console.log message here, information about the player. If I think, well, I want to write out the name of the object, but I don't know whether I'm going to be doing player1.name or player2.name. I just want to do one at a time, and I could call this for either. Well, here's the thing that we do. If I know this function is going to be associated with different objects, what I can use is the word this to refer to the current one.

So whatever object it is, I'm going to go into its name property, or its score property, or its rank property. So I'll put together a bit more of a message, this.name and a little string has a rank of, and then we use the concatenation and just to make it clear I'll split it across a few lines, this.rank. Close the console.log parentheses, close the semicolon. Now, right now if I ran this code, nothing would actually happen, because I've defined the function, I'm creating the objects, but we don't have any information coming out.

What I can do sometime later in the code is finally call the logDetails function of player1 and the logDetails function of player2. And we're going to use the parentheses to let JavaScript know, yes, we're actually calling it now. So I'm going to go ahead and run this, and we get the message out here, Fred has a rank of 1 and a score of 10000. Because it's calling it on Fred, it's jumping and saying, "What does this logDetails do?" Oh, well, it calls playerDetails, and that's this function here, which for the current object, this.name is Fred, this.rank is 1, this.score is 10000.

And I could just call that for the second object, and then we get the second message out, which is that object's information. For those of you coming from more formal object-oriented languages, you're almost certainly recognizing that JavaScript seems very casual about this. There's not a lot of rules being enforced here. And yes, that's true. We can just kind of go ahead and make these objects and tie functions to them. It all seems quite loose and flexible. Well, that's JavaScript for you.

And know that JavaScript doesn't have formal classes as other object-oriented languages do, but you can emulate the same effect using a normal function and something called a prototype, but that's not necessary for us right now. So I'm going to leave it until later on in the course. Because most people most of the time don't need to get really deep into object orientation, this will be more than enough to get us started. Because knowing this, knowing that the idea of objects, the idea of properties, the idea of methods, allows us to move on and start working with the really important objects in JavaScript, the window object and particularly the document object, the most important object for any JavaScript developer.

Show transcript

This video is part of

Image for JavaScript Essential Training
JavaScript Essential Training

56 video lessons · 109955 viewers

Simon Allardice
Author

 
Expand all | Collapse all
  1. 3m 28s
    1. Welcome
      1m 1s
    2. What you should know
      1m 44s
    3. Using the exercise files
      43s
  2. 15m 41s
    1. Introduction to JavaScript
      8m 6s
    2. Creating your first JavaScript
      2m 13s
    3. Getting to know the tools and applications
      5m 22s
  3. 56m 8s
    1. Understanding the structure of JavaScript code
      7m 9s
    2. Where to write your JavaScript
      3m 56s
    3. Creating variables
      6m 21s
    4. Working with conditional code
      5m 44s
    5. Working with operators
      13m 28s
    6. Sending messages to the console
      2m 59s
    7. Working with loops
      8m 1s
    8. Creating functions
      8m 30s
  4. 36m 13s
    1. Working with arrays
      7m 57s
    2. Working with numbers
      6m 13s
    3. Working with strings
      8m 27s
    4. Working with dates
      5m 38s
    5. Working with objects
      7m 58s
  5. 9m 6s
    1. What is the DOM?
      5m 49s
    2. Working with nodes and elements
      3m 17s
  6. 25m 17s
    1. Accessing DOM elements
      11m 3s
    2. Changing DOM elements
      5m 42s
    3. Creating DOM elements
      8m 32s
  7. 24m 45s
    1. Introduction to JavaScript event handling
      8m 16s
    2. Working with onClick and onLoad events
      7m 36s
    3. Working with onBlur and onFocus events
      2m 36s
    4. Working with timers
      6m 17s
  8. 21m 41s
    1. Common JavaScript errors
      7m 14s
    2. Using Firebug
      4m 7s
    3. Going through a debugging session
      10m 20s
  9. 10m 13s
    1. Accessing form elements
      4m 20s
    2. Preventing a form from being submitted
      2m 36s
    3. Hiding and showing form sections
      3m 17s
  10. 9m 49s
    1. CSS and JavaScript
      3m 46s
    2. Removing and applying CSS classes
      2m 16s
    3. Changing inline styles
      3m 47s
  11. 19m 44s
    1. Understanding JavaScript style
      7m 39s
    2. Minifying your code
      4m 28s
    3. Using JavaScript code checkers
      7m 37s
  12. 22m 24s
    1. Introduction to JavaScript libraries
      3m 17s
    2. Linking to multiple JavaScript files
      2m 11s
    3. Introduction to jQuery
      12m 7s
    4. Using a content distribution network to deliver JavaScript files
      4m 49s
  13. 17m 35s
    1. JavaScript in HTML5
      9m 37s
    2. Using Modernizr
      3m 2s
    3. Using Strict Mode
      4m 56s
  14. 33m 3s
    1. Knowing the JavaScript to avoid
      6m 35s
    2. Introduction to regular expressions
      6m 56s
    3. Working with AJAX
      10m 44s
    4. Working with objects and prototypes
      8m 48s
  15. 21m 10s
    1. Example: Countdown
      8m 3s
    2. Example: Resize
      5m 47s
    3. Example: Accordion
      7m 20s
  16. 4m 58s
    1. Where to go from here
      4m 0s
    2. Goodbye
      58s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed JavaScript Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.