-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs18.js
More file actions
89 lines (48 loc) · 5.28 KB
/
Copy pathjs18.js
File metadata and controls
89 lines (48 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
Object Methods
You might already be familiar with objects in JavaScript.
In this video, you will learn how to design objects as combinations of data and functionality.
As you might already know, an object consists of key-value pairs, known as properties.
We can add new key-value pairs to existing objects using the dot notation and the assignment operator.
21
car.color = "red"; //update the value of a property of the car objjectvar car = {};
These are known as properties, and can take many data types, including functions.
12345678
var car = {}; car.color = "red"; //add a method to the car object so that it can be called as car.turnkey()car.turnKey = function() { console.log('engine running'); }
If the function is a property of an object, it is then referred to as a method.
This is a function that can be accessed only through the JavaScript object that it is a member of. For example, the log method, which belongs to the console object, can only be accessed through the console object.
console.log('Hello world');
Let's explore this further now. I will create an object using something known as the constructor function.
1234
var car = {};car.mileage = 98765;car.color = "red";console.log(car);
Reset
First, I'll build a new object literally named car. I type var, space, car, space, equal sign, space, followed by a set of curly braces, and finally a semicolon.
Now, I'll extend the car object by assigning it a property named mileage.
When I inspect the object, I can confirm that it contains a mileage property set to 98765.
I want to add another property to the car object. This time, I will add a property named color and set it to the value of "red".
I can inspect the object again by typing its name into the browser console. So now, when I type console.log(car), I get an object with two properties: the mileage property, which is set to 98765, and the color property, set to "red".
Great, now I've added two properties to my object.
Next, I want to add a method to my car object. And this method, when called, will output some text to the console.
So, once again, I add another property to my car object. After all, a method is just another property of the car object. It's just another key-value par that the car object holds.
What's unique is that the value I'm assigning to it is a function.
12345678
var car = {};car.mileage = 98765;car.color = "red";console.log(car);car.turnTheKey = function() { console.log("The engine is running")}console.log(car);
Reset
So, I begin by typing car dot turnTheKey, equals, and then I type the code for my function. So function, open-close parentheses. Then the two curly braces where I will place my code. Finally, inside the curly braces, I type the console dot log followed by the message "The engine is running".
Now I can inspect my car object again by typing its name into the console log method. This time, it displays that the car object contains three properties; the color property, the mileage property, and the turnTheKey property.
Remember that all the key-value pairs in an object are referred to simply as properties. However, if I want to differentiate between the properties that can be executed, then I refer to such properties as methods.
So, now I want to add another method to the car object. I'll name this one lightsOn.
Once again, I type car.lightsOn, and then I add an equals sign, and again since it's a method, I'm assigning it to a function. This function will also have a console log in its body, and I'm just logging the string with the text "The lights are on".
1234567891011121314
//example of adding properties and methods to an objectvar car = {};car.mileage = 98765;car.color = "red";console.log(car);car.turnTheKey = function() { console.log("The engine is running")}car.lightsOn = function() { console.log("The lights are on.")}console.log(car);car.turnTheKey();car.lightsOn()
Reset
Ok, so now I have added four properties to my object. And two of those are methods.
I've already ensured that I'm getting the correct mileage and color from my car object. Now, I'll try executing the turnTheKey and the lightsOn methods.
First, I'll invoke the turnTheKey method.
Remember that this method can be accessed only through the car object, so I first need to type the name of the object that holds the turnTheKey method. In other words, I need to type the word car, followed by a dot, and then the name of my method, which is turnTheKey.
Remember that this property is a method. So, to run it, I need to append an opening and a closing parenthesis so that the JavaScript engine can process my JavaScript code.
Notice that this results in the "The engine is running" string logged to the console.
Now I'll test the other method. Once again, I need to access it through the car object, so I type car.lightsOn, and again, I need to add those parentheses to invoke the lightsOn method. I press the ENTER key and notice the text displays in the console.
Success! It's important to remember that when the JavaScript engine runs this line of code, it locates the car object in its memory. Then, it finds the lightsOn method on the car object. It reads the function declaration that's saved on this property and runs it, line by line.
Since there's only a single line of code, the JavaScript engine logs the string "The lights are on" to the console.
*/