-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.js
More file actions
105 lines (88 loc) · 3.14 KB
/
objects.js
File metadata and controls
105 lines (88 loc) · 3.14 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
// object and object method
// methods are functions stored as object properties
let person1 = {
name: "Sampad",
age: 28,
'is Scholar': true,
personInf: function() {
return `${this.name} is ${this.age} years old Scholar`
// return `${person1.name} is ${person1.age} years old Scholar`
// console.log(`${this.name} is ${this.age} years old Scholar`)
},
}
console.log(person1)
console.log(person1.name)
console.log(person1.personInf()) // personInf is method of the object person1. it is a function stored as object property
console.log(person1['age'])
console.log(person1['is Scholar']) // in that case dot(.) operator does not work
// when object property is asked to user through an input, then dot(.) operator does not work
let input = 'name'
console.log(person1[input]) // no dot operator
/*
// Creating empty object and then adding properties in it
// let person1 = {}
const person1 = {}
person1.name = "Sampad"
person1.age = 28
person1['is Scholar'] = true
person1.personInf = `${person1.name} is ${person1.age} years old Scholar`
// person1.personInf = `${this.name} is ${this.age} years old Scholar` // this does not work here as this._ is undefined
console.log(person1)
*/
/*
// using new Object()
const person1 = new Object()
person1.name = "Sampad"
person1.age = 28
person1['is Scholar'] = true
person1.personInf = `${person1.name} is ${person1.age} years old Scholar`
console.log(person1)
*/
/*
let person1 = {
name: "Sampad",
age: 28,
'is Scholar': true,
personInf: function() {
// return `${this.name} is ${this.age} years old Scholar of ${this.research.area}`
return `${person1.name} is ${person1.age} years old Scholar of ${person1.research.area}`
},
research: {
area: "Computational material science",
topic: "Thermoelectricity",
exp: "3 years",
},
// personInf: function() {
// // return `${this.name} is ${this.age} years old Scholar of ${this.research.area}`
// return `${person1.name} is ${person1.age} years old Scholar of ${person1.research.area}`
// },
}
console.log(person1.personInf())
// console.log(person1.research)
// console.log(person1.research.topic)
// console.log(person1.research.topic.length)
// console.log(person1.research.topics.length) // topics does not exist
// console.log(person1.research.topics?.length) // ? checks the existence of "topics" and absence is indicated by "undefined"
*/
/*
// deleting object property
const person1 = {
name: "Sampad",
age: 28,
'is Scholar': true,
personInf: function() {
return `${this.name} is ${this.age} years old Scholar`
},
research: {
area: "Computational material science",
topic: "Thermoelectricity",
exp: "3 years",
department: "Chemistry",
university: "Visva-Bharati"
},
}
console.log(person1)
// delete person1.personInf // deleting object property
delete person1.research.exp // deleting object property inside object
console.log(person1)
*/