-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9 objects.js
More file actions
171 lines (118 loc) · 3.75 KB
/
9 objects.js
File metadata and controls
171 lines (118 loc) · 3.75 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
***********JavaScript Objects*************
In real life, a car is an object.
A car has properties like weight and color, and methods like start and stop:
All cars have the same properties, but the property values differ from car to car.
All cars have the same methods, but the methods are performed at different times.
*/
/*
let car= {tupe:"fiat",midel:"500",color:"white"};
console.log(car.color);
const persen1={
fname:"RANA",
lname:"ABOBAKAR",
age:18,
fullname:function()
{
return this.fname+ " " +this.lname;
}
};
console.log("full name",persen1.fullname());
/*
Do Not Declare Strings, Numbers, and Booleans as Objects!
When a JavaScript variable is declared with the keyword "new", the variable is created as an object:
x = new String(); // Declares x as a String object
y = new Number(); // Declares y as a Number object
z = new Boolean(); // Declares z as a Boolean object
/*
Avoid String, Number, and Boolean objects. They complicate your code and slow down execution speed.
*/
//************** Accessing Object Properties with Dot Notation ***********/
const testObj = {
"hat": "ballcap",
"shirt": "jersey",
"shoes": "cleats"
};
console.log(testObj.hat)
/**************************Accessing Object Properties with Bracket Notation */
console.log(testObj["shirt"])
/**************Accessing Object Properties with Variables */
let name="hat";
console.log(testObj[name]);
/*******************Updating Object Properties */
testObj.hat="Rana"
console.log(testObj);
/**********Add New Properties to a JavaScript Object */
testObj.hard="something"
// or
testObj["GG"]="GG";
console.log(testObj);
/*********Delete Properties from a JavaScript Object */
delete testObj.GG;
console.log(testObj);
/**************Using Objects for Lookups**********************
Objects can be thought of as a key/value storage, like a dictionary. If you have tabular data,
you can use an object to lookup values rather than a switch statement or an if/else chain.
This is most useful when you know that your input data is limited to a certain range.
Here is an example of a simple reverse alphabet lookup:
*/
const alpha = {
1:"Z",
2:"Y",
3:"X",
4:"W",
24:"C",
25:"B",
26:"A"
};
alpha[2];
alpha[24];
const value = 2;
alpha[value];
// Setup
function phoneticLookup(val) {
let result = "";
let lookup={
alpha:"Adams",
bravo:"Boston",
charlie:"Chicago",
delta:"Denver",
echo:"Easy",
foxtrot:"Frank"
}
// Only change code below this line
result= lookup[val]
// Only change code above this line
return result;
}
phoneticLookup("charlie");
/*********************************Testing Objects for Properties***************
Sometimes it is useful to check if the property of a given object exists or not. We can use the .hasOwnProperty(propname) method of objects to determine if that object has the given property name. .hasOwnProperty() returns true or false if the property is found or not.
Example
*/
const myObj = {
top: "hat",
bottom: "pants"
};
myObj.hasOwnProperty("top");
myObj.hasOwnProperty("middle");
//The first hasOwnProperty returns true, while the second returns false
// *TODO: Object Destructuring in ES6
// Similar to Array destructuring, Object destructuring unpacks properties into distinct variables.
// For example:
let obj={h:100,s:true}
let {h,s}=obj;//Use same name of property of object
console.log(h);
console.log(s);
let {a,b}=obj;//get error .
// *You can also assign the object to new variable names.
// For example:
let obj1={a:1000,b:false}
let {a:C,b:D}=obj1;
console.log(C,D);
// Finally you can assign default values to variables, in case the value unpacked
// from the object is undefined.
// For example:
let obj2={W:1000,R:false}
let {Q=10,W=12,R=true}=obj2;
console.log(Q,W,R);