-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects.js
177 lines (120 loc) · 3.09 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
let b={};
// a is the object
let a={
"yash":"web dev",
"ayush":"app dev",
"vaibhav":"block dev"
}
let key="kaka"
a[key]="react_js";
// a new value pair can be added like this.
// console.log(a);
// <----------------------------------------question-------------------------------------------->
delete a.kaka;
// console.log(a);
if(isEmpty(a)==false){
// console.log("fill");
}
else{
// console.log("empty");
}
function isEmpty(obj){
for(let key in obj){
return false;
}
return true;
}
// <----------------------------------------question-------------------------------------------->
let salaries = {
John: 100,
Ann: 160,
Pete: 130
};
// console.log(sum(salaries));
function sum(obj){
let sum=0;
for (let key in obj){
sum+=obj[key];
}
return sum;
}
//<----------------------------------------question-------------------------------------------->
// before the call
let menu = {
width: 200,
height: 300,
title: "My menu"
};
multiply(menu);
// console.table(menu);
// used table for arranging the key value pairs in the table.
function multiply(obj){
for(let key in obj){
if(typeof obj[key]=='number'){
obj[key]*=2;
}
}
}
//<----------------------------------------question-------------------------------------------->
// Arrays.prototype.map();
// using the constructor.
function Food(width,height,title){
this.width=width;
this.height=height;
this.title=title;
this.greet=function(){
// console.log("hello");
}
}
const l=new Food(23,22,"hello");
// object.
// console.log(l.width);
// console.log(l.height);
// using the constructors.
l.greet();
//<----------------------------------------question-------------------------------------------->
function info(title,author,pages,cond){
this.title=title;
this.author=author;
this.pages=pages;
this.cond=cond;
this.check= function(){
if(this.cond){
console.log("Read");
}
else{
console.log("Not Read")
}
}
}
const book=new info("hell","yash",206,true);
// book.check();
// console.log(book.title);
// console.log(book.author);
// console.log(book.pages);
//<----------------------------------------question-------------------------------------------->
// what are prototypes in js.(Later--on);
// prtotypes are the shareable objects ,its saves alots of memory. Can be accessed by any number of instances.
function Apple(type,price){
this.type=type;
this.price=price;
}
// Apple is the blue-print.--having the general properties.
const apple1=new Apple("Russian",330);
const apple2=new Apple("American",456);
// Sysntaxof making the prototype objects.
Apple.prototype.welcome=function(){
console.log("welcome to the store");
}
Apple.prototype.Leave=function(){
console.log("Bye..bye");
}
apple1.welcome();
console.log(apple1.type);
console.log(apple1.price);
apple1.Leave();
apple1.welcome();
console.log(apple2.type);
console.log(apple2.price);
apple1.Leave();
// object.create.