-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectFunctions.js
More file actions
105 lines (69 loc) · 2.59 KB
/
ObjectFunctions.js
File metadata and controls
105 lines (69 loc) · 2.59 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
const person = {
personname:"Prajwal",
}
//here from assign method we can reassign , change , add new property to the Object it will change the orginal Object ie here person object;
Object.assign(person, {
personname:"raju",
age:24
});
console.log( person);
//to avoid changing the orginal object we can return the Object to another variable by declarations
const newPerson = Object.assign({},person,{
personname:"sajna",
age:24
})
console.log(person);
//this is to copy object using spread operator
const copyPerson = {...person};
console.log(copyPerson);
//now lets try to reaasign something or add properties using spread operator to new person object;
const anotherPerson = {...person,...{
personname:"jeffry",
age:"45",
company:"Blue Orgigin"
}}
console.log(`old object is : ${person} \n new object is ${anotherPerson} `);
/* --------------------------------------------------------------------------------------------------------------------- */
const animal = {}
Object.defineProperty(animal,"name",{
value:"Cat",
writable: false
});
console.log(animal.name);
Object.defineProperty(animal,"date",{
get() {
return new Date();
}
})
console.log(animal.date);
//defineProperty in 3rd argument where we define descriptor that is properties we can also define function , and inside descriptor we can use value, writable or get, set
// here we can only use get to return a function , and set to set something ,
Object.defineProperty(animal,"demoFunction",{
get(){
return function demo(){
console.log("You just hit this function");
}
}
})
animal.demoFunction();
/*-------------------------------------------------------------------------------------------------------------------------------------*/
const weather = {
loacation:"Banglore",
temp:35,
wind: "12kn west"
}
const array = Object.entries(weather); //entries method is used to return array from object it returns multi dimensional array;
console.log(array);
const samagri = [['apple',5],['orange',6],['cherry',10]];
const samagriObject = Object.fromEntries(samagri); //fromEntries method is used to convert multiDim array to object , it return object from an array passed
console.log(samagriObject);
/*-------------------------------------------*/
/* we can also also make object immutbale that is we cant delete , add or reassign by using freeze method */
const sampleObject = {
name:"prajwal",
age:21,
college:"dropout"
}
Object.freeze(sampleObject);
delete sampleObject.name;
console.log(sampleObject); //property will not be deleted