-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.js
More file actions
132 lines (91 loc) · 3.17 KB
/
object.js
File metadata and controls
132 lines (91 loc) · 3.17 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// creating objects and retrieving values from them.
const userProfile = {
name: "John Doe",
email: "john.doe@example.com",
address: {
city: "New York",
street: "123 Main St",
zipCode: "10001"
}
};
const userProfile2 = {
name: "Marry Jane",
email: "marry.jane@example.com",
address: {
city: "L.A",
street: "154, washington road",
block: '0101',
zipCode: "10011"
}
};
function getUserDetail(profile, keys) {
// Implement your function here
let varkey = profile;
for (let key of keys){
varkey = varkey[key];
}
return varkey;
}
console.log(getUserDetail(userProfile2, ["address", "block"]));
// creating similiar objects like this is not optimised, better to this with function like constructor function.
// end of creating objects and retrieving values from them.
// constructor function
function Movie (title,year){ /* Constructor function name starts with Capital letter*/
this.title = title;
this.year = year;
this.getDetail = function(){
console.log( `Title : ${this.title} , Year : ${this.year}`);
};
}
const movie123 = new Movie('Hello','1234');
movie123.getDetail();
// end constructor function
// prototype
// add the method to the prototype chain instead of creating copies.
// Ensure that isClassic method is accessible to all instances of Book object.
function Book(title, author, publicationYear) {
this.title = title;
this.author = author;
this.publicationYear = publicationYear;
Book.prototype.isClassic = function () {
this.currentYear = 2023;
this.age = this.currentYear - this.publicationYear;
if (this.age >= 50) {
return "The book is from the classic collection";
}
return "The book is not a classic collection book";
};
}
// Create a few book objects
const book1 = new Book("To Kill a Mockingbird", "Harper Lee", 1990);
const book2 = new Book("1984", "George Orwell", 1949);
const book3 = new Book("The Great Gatsby", "F. Scott Fitzgerald", 1925);
// Consoling output
console.log(book1.isClassic()); // Output: 63
console.log(book3.isClassic()); // Output: 98
// end prototype
// bind
/**modify the login method by using the bind method to create a
* reusable function named loginFunction that is bound to the systemCredentials object. */
function User(displayName) {
this.displayName = displayName;
}
const systemCredentials = {
username: "system",
password: "pass123"
};
User.prototype.login = function (username, password) {
// Implement the code here
if (this.username === username && this.password === password){
console.log('Login successful!');
}else{
console.log('Login failed!');
}
};
// Example usage:
const user = new User("John Doe");
// Create the reusable loginFunction here
let loginFunction = user.login.bind(systemCredentials);
loginFunction("system", "pass123"); // Expected output: "Login successful!"
loginFunction("wrongUsername", "wrongPassword"); // Expected output: "Login failed!"
// end bind