-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.js
More file actions
48 lines (32 loc) · 1.08 KB
/
objects.js
File metadata and controls
48 lines (32 loc) · 1.08 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
const parent = {
name: "Samrood",
};
const child = Object.create(parent);
child.age = 26;
// check if a property exists in an object
console.log("name" in child); // true // 'in' checks both parent and child
console.log("age" in child); // true
console.log(child.hasOwnProperty("name")); // false //checks only 'own' object, child here.
console.log(child.hasOwnProperty("age")); // true
// 'in' checks inheritance also but 'hasOwnProperty' only checks the target object and not it's parents
console.log("toString" in child); //true because toString is available in every object
// GETTERS AND SETTERS
{
const obj = {
firstName: "Samrood",
lastName: "Ali",
// get propertyName
get fullName() {
return this.firstName + " " + this.lastName;
},
// set propertyName with the argument being the value assigned
set fullName(value) {
const [firstName, lastName] = value.split(" ");
this.firstName = firstName;
this.lastName = lastName;
},
};
console.log(obj.fullName);
obj.fullName = "Abhiram Vishnu";
console.log(obj.fullName);
}