1
00:00:01,016 --> 00:00:02,503
Let's start by taking a look at one of the

2
00:00:02,503 --> 00:00:04,299
simplest and most common ways to create

3
00:00:04,299 --> 00:00:08,673
objects in JavaScript, the object literal.

4
00:00:08,673 --> 00:00:10,659
Actually, before we do that, let's just

5
00:00:10,659 --> 00:00:13,180
talk briefly about objects. Objects are

6
00:00:13,180 --> 00:00:15,280
basically just more complex variable

7
00:00:15,280 --> 00:00:18,327
types. So for example, we could have a

8
00:00:18,327 --> 00:00:20,641
couple of variables like this, and imagine

9
00:00:20,641 --> 00:00:23,220
if every time you wanted to describe a

10
00:00:23,220 --> 00:00:25,653
person, first name, last name, age, eye

11
00:00:25,653 --> 00:00:28,767
color, hair color, you had to declare

12
00:00:28,767 --> 00:00:31,173
individual variables and pass each one of

13
00:00:31,173 --> 00:00:33,228
those variables around in your program. An

14
00:00:33,228 --> 00:00:35,954
object is essentially a way of just

15
00:00:35,954 --> 00:00:37,661
grouping information like this together

16
00:00:37,661 --> 00:00:40,566
into a single variable. So instead of

17
00:00:40,566 --> 00:00:42,469
multiple variables, I can have a person

18
00:00:42,469 --> 00:00:43,736
object, and I can just pass that person

19
00:00:43,736 --> 00:00:46,618
object around in my code just like I would

20
00:00:46,618 --> 00:00:48,833
any other variable. But in doing so, I'd

21
00:00:48,833 --> 00:00:50,668
be passing all this information around. Of

22
00:00:50,668 --> 00:00:53,518
course objects can also be used to

23
00:00:53,518 --> 00:00:55,447
encapsulate not just information, but

24
00:00:55,447 --> 00:00:58,747
functionality related to that object using

25
00:00:58,747 --> 00:01:01,013
methods. We'll get to that in a bit. For

26
00:01:01,013 --> 00:01:03,110
now, let's see how to turn this data into

27
00:01:03,110 --> 00:01:05,695
an object. The easiest way to do that in

28
00:01:05,695 --> 00:01:08,319
JavaScript is with an object iteral. So

29
00:01:08,319 --> 00:01:10,041
first I'm going to create a variable

30
00:01:10,041 --> 00:01:11,724
called person, and then I'm going to set

31
00:01:11,724 --> 00:01:14,259
that variable equal to a new object, and

32
00:01:14,259 --> 00:01:17,757
this is where the object literal comes in.

33
00:01:17,757 --> 00:01:20,115
To create an object with an object

34
00:01:20,115 --> 00:01:21,821
literal, you simply define the properties

35
00:01:21,821 --> 00:01:27,119
and their values inside braces like this.

36
00:01:27,119 --> 00:01:29,397
So here I've created a new object named

37
00:01:29,397 --> 00:01:32,111
person with two properties, firstName and

38
00:01:32,111 --> 00:01:35,277
lastName. And notice if I call our display

39
00:01:35,277 --> 00:01:38,661
function and print out person.Name, if I

40
00:01:38,661 --> 00:01:42,237
save that, it displays Jim. Well that was

41
00:01:42,237 --> 00:01:43,654
easy, now I have this person object with

42
00:01:43,654 --> 00:01:46,563
firstName and lastName properties on it.

43
00:01:46,563 --> 00:01:48,381
And I can pass this person object around

44
00:01:48,381 --> 00:01:50,179
in my code, for example, say I had a

45
00:01:50,179 --> 00:01:52,642
registerUser function, and I want to pass

46
00:01:52,642 --> 00:01:55,416
in this person into that function, I could

47
00:01:55,416 --> 00:01:57,109
do that like this. I just call

48
00:01:57,109 --> 00:01:58,910
registerUser, and pass in person as a

49
00:01:58,910 --> 00:02:01,099
parameter just like I would any other

50
00:02:01,099 --> 00:02:03,650
variable. So that's the easiest way to

51
00:02:03,650 --> 00:02:06,071
create objects in JavaScript using an

52
00:02:06,071 --> 00:02:09,283
object literal like this. Next, let's talk

53
00:02:09,283 --> 00:02:13,000
about the dynamic nature of objects in JavaScript.

