-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08-objects.html
More file actions
136 lines (109 loc) · 4.33 KB
/
08-objects.html
File metadata and controls
136 lines (109 loc) · 4.33 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
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8">
<title>Objects</title>
</head>
<body>
<!--<p><div>
This is a page for the objects in JS.
We are creating a simple object for a product.
The object has two properties: name and price.
</div></p>-->
<!--</div>page for the objects in JS</p>-->
<script>
/* const product={
name:'socks',
price:1090
// name:'shoes',
// price:1200
};
console.log(product);
//console.log("how are u");
console.log(product.name);
console.log(product.price);
product.name ='cotton socks';
console.log(product.name);
console.log(product);
product.newProperty = true;
console.log(product);
delete product.newProperty;
console.log(product);*/
/*
const product2={
name:'shirt',
'delivery-time': '1 day',
rating :{
stars:4.5,
count: 97 //nested object example
},
fun: function function1(){
console.log('this is a function');
//console.log(this); // this refers to the object calling the function
}
};
console.log(product2);
console.log(product2.name);
console.log(product2.name.length);//gives the length of object
console.log(product2['name']);// bracket notation similar to dot -->'.' notation
product2['price'] = 1200;//this is mainly used when dot notation properties not worked
//console.log(product2.delivery-time);-->produce errors
//08-objects.html:47 Uncaught ReferenceError: time is not defined i.e; delivery-time doesnot exsist in the object
//console.log(product2.delivery-time);
console.log(product2['delivery-time']);
console.log(product2);
console.log(product2.price);
console.log(product2.rating.count);
console.log(product2.rating.stars);//to access values in the nested
//console.log(product2.rating.fun);
product2.fun();
console.log(typeof console);//it says the type of the given parameter..
console.log(typeof console.log);
console.log(JSON.stringify(product2));// this method used to convert the java script object to JSON object
// alert(JSON.stringify(product2));
const jsonString=JSON.stringify(product2);
const product3=JSON.parse(jsonString);// this function is used to convert JSON file to java script object
console.log(product3); */
console.log('hello'.length);
console.log('hello'.toUpperCase());
console.log('hello'.toLowerCase());
console.log('hello'.charAt(0));
//console.log(toUpperCase('hello'));
const object1={
message:'hello'
};
const object2=object1;//copy by reference ie; here while assiging the object 1 value to object 2 not whole object will be dupliciated only the object 1 references will be stored in the obj2;
console.log(object1.message);
console.log(object2.message);
// object2.message='world';
//console.log(object1.message);
object1.message ='Good Job!';
console.log(object1);
const object3={
message:'hello'
};
console.log(object1===object3); // false because two objects are not same they have different references
console.log(object1===object2); // true because two objects are same they have different
console.log(object1.message === object3.message);
const object4={
message:'Good Job!',
price: 799
};
// const message =object4.message; //
const { message, price } =object4;//destructing
console.log(message);
console.log(price);
const object5 ={
// message: message
message ,//short hand method here copying all the values in the another object to the new object which is known as the short hand method
// method: function function1(){
// console.log('method');
//}
method(){
console.log('method');
}
};
console.log(object5);
object5.method();
</script>
</body>
</html>