forked from Pankaj-Str/JavaScript-Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.js
More file actions
100 lines (70 loc) · 1.56 KB
/
object.js
File metadata and controls
100 lines (70 loc) · 1.56 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
const person = {
firstName: "John",
lastName: "Doe",
age: 30,
isStudent: false
};
console.log(person.firstName)
// class and object
class client{
info(){
console.log("welcome to p4n.in");
}
}
// call from object
obj = new client();
obj.info();
// object create method Object.create()
const animal = {
type: "Mammal",
sound: "Roar"
};
const lion = Object.create(animal);
lion.name = "Simba";
console.log(lion.name)
console.log(lion.type)
// Object Methods
const calculator = {
add: function(a, b) {
return a + b;
},
subtract: function(a, b) {
return a - b;
}
};
console.log(calculator.add(5, 3)); // 8
console.log(calculator.subtract(8, 2)); // 6
const p4n_calculator = {
gst: function(price, gst) {
finalprice = price*gst/100
finalprice = price+finalprice
console.log("final price : "+finalprice)
}
};
p4n_calculator.gst(1200,18);
// Object Properties and Methods
const student = {
firstName: "Alice",
lastName: "Smith",
age: 20,
greet: function() {
console.log(`Hello, I'm ${this.firstName} ${this.lastName}.`);
}
};
student.greet(); // "Hello, I'm Alice Smith."
// Object Iteration
const person1 = {
name: "John",
age: 20,
};
console.log(person1);
for (let key in person1) {
console.log(key, person1[key]);
}
person1.age = 33;
for (let key in person1) {
console.log(key, person1[key]);
}
const keys = Object.keys(person1);
const values = Object.values(person1);
const entries = Object.entries(person1);