-
Notifications
You must be signed in to change notification settings - Fork 0
/
objects.js
107 lines (74 loc) · 1.92 KB
/
objects.js
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
//object creation
//way-1
const student={
name: "Vivek",
rollNo: "105",
section: 2,
branch : "CSE"
};
console.log("The object is " ,student);
//accessing object properties
let ObjName= student.name;
let ObjBranch = student['branch'];
console.log("The accessed student name is",ObjName,"branch is" ,ObjBranch);
//way-2 (creating object)
var car=new Object();
car.name ="Fortuner";
car.brand="Toyota";
car.modelYear="2020";
console.log(car);
//way 3 (object constructor method)
function stud(id,name,branch){
this.id=id;
this.name=name;
this.branch=branch;
} //The this keyword refers to the current object.
let s=new stud(103,"vivek","CSE");
console.log(s);
//creating objects with methods
const person = {
firstName: "Vivek",
lastName: "Potla",
language: "English",
lang() {
return this.language;
}
};
console.log(person.lang())
//Javascript object methods
// Initialize an object with properties and methods
const SampleObject = {
name:"FakeObject",
num:36,
showDetails() {
console.log(this.name , this.num);
}
};
// Use Object.create to pass properties
const Obj1 = Object.create(SampleObject);
Obj1.showDetails();
// Initialize an object
const employees = {
boss: 'John',
secretary: 'Kepler',
sales: 'Gandhi',
accountant: 'Nehru'
};
// Get the keys of the object
const keys = Object.keys(employees);
console.log(keys);
//know the length of keys
const length = Object.keys(employees).length;
console.log(length);
//acess the values of objects
const values = Object.values(employees);
console.log(values);
// Object.entries() creates a nested array of the key/value pairs of an object.
const entries = Object.entries(employees);
console.log(entries);
// Loop through the results using entries as entries is now an array
entries.forEach(entry => {
let key = entry[0];
let value = entry[1];
console.log(`${key}: ${value}`);
});