-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_ObjectOrientedProgramming.js
More file actions
83 lines (65 loc) · 2.24 KB
/
08_ObjectOrientedProgramming.js
File metadata and controls
83 lines (65 loc) · 2.24 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
// Introduction to JavaScript
// Lesson 8: Object-Oriented Programming
// Basics of JS Objects
// Objects are used to store multiple values in a single variable.
// Objects are created using curly braces {}.
// Objects can contain any data type, including numbers, strings, and arrays.
// Objects can be accessed using dot notation . or bracket notation [].
// Example of an object
const friendo = {
name: 'Alice',
age: 30,
hobbies: ['reading', 'cooking'],
};
// Example of accessing an object property
console.log(friendo.name); // Prints 'Alice'
// Example of changing an object property
friendo.name = 'Bob';
console.log(friendo.name); // Prints 'Bob'
// Example of adding a property to an object
friendo.height = 180;
console.log(friendo.height); // Prints 180
// Example of removing a property from an object
delete friendo.height;
console.log(friendo.height); // Prints 'undefined'
// Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects".
// Creating a simple object with methods
let dog = {
name: 'Buddy',
breed: 'Golden Retriever',
describe() {
return `${this.name} is a ${this.breed}`;
},
};
console.log(dog.describe());
// Wait, what the heck is 'this'?
// 'this' is a keyword that refers to the current object.
// In the example above, 'this' refers to the 'dog' object.
// CHALLENGES
// CHALLENGE 1: Create an object
// Create an object 'car' with properties 'make', 'model', and a method 'display' that returns a string combining make and model.
// TODO: Create the 'car' object here.
// CHALLENGE 2: Add a method to an object
// Add a method 'getAge' to the 'person' object that returns the person's age.
let person = {
name: 'Alice',
birthYear: 1995,
// TODO: Add the 'getAge' method here.
};
// CHALLENGE 3: Fix the method
// The method 'greet' should print "Hello, my name is [name]". Fix it to correctly use the 'this' keyword.
let user = {
name: 'Bob',
greet() {
// TODO: Fix the line below to correctly use 'this'
return 'Hello, my name is ' + name;
},
};
console.log('End of Object-Oriented Programming');
module.exports = {
car,
person,
user,
};
// To run this JavaScript file, use the command 'node 08_ObjectOrientedProgramming.js' in your terminal.
// ✅ REMOVE THIS LINE to check your work