-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL1.js
More file actions
200 lines (165 loc) · 5.17 KB
/
L1.js
File metadata and controls
200 lines (165 loc) · 5.17 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Object base language: javascript (not object oriented)
// Object - oriented languages do not have the inbuilt objects
// whereas Object - based languages have the inbuilt objects
// dynamically typed language
var a = 10;
var b = "Hello";
var c = true;
console.log(".......function.........");
// function
function f1(arg1) {
arg1 = arg1 + 10;
console.log(arg1); // 20
}
console.log(".......Pass by value.........");
console.log(a);
f1(a);
console.log(a);
// same output -> pass by value -> value of "a" not get manupulated
// function - pass by reference
// Object literal notation
var obj = {
a: 10
};
function f2(arg1) {
arg1.a = arg1.a + 10;
console.log(arg1.a); // 20
}
console.log(".......Pass by reference.........");
console.log(obj.a);
f2(obj);
console.log(obj.a);
// output get updated here -> pass by reference -> value of "obj.a" get manupulated
// All datatypes which are object type -> pass by reference
// All datatypes which are primitive type -> pass by value
// To use pass by reference -> use object literal notation -> var obj = {};
// To use pass by value in object -> use deep copy -> var obj = Object.assign({}, obj);
console.log(".......Deep Copy.........");
f2(Object.assign({}, obj)); // 30
console.log(obj.a); // 20
// Shallow cloning -> only copies the first level of the object
// Deep cloning -> copies the entire object
var obj2 = {
a: 10,
b: {
name: "John"
},
c: [1, 2, 3]
};
function ShallowClone(obj) {
var clone = {};
for (var key in obj) {
clone[key] = obj[key];
}
return clone;
}
function DeepClone(obj) {
var clone = {};
for (var key in obj) {
if (typeof obj[key] === "object") {
clone[key] = DeepClone(obj[key]);
} else {
clone[key] = obj[key];
}
}
return clone;
}
// new keyword for creating new object -> using "constructor function" (earlier we used object literal notation)
var obj3 = new Object();
obj3.name = "John";
obj3.age = 30;
// both are same -> open for modification/extension
// "New" keyword is used to create new object -> it assign a new memory address to the object
// Custom Types
function Product(name, brand) {
this.name = name;
this.brand = brand;
}
var p1 = new Product("Iphone", "Apple");
// When we create a new object using "new" keyword -> it automatically creates a new memory address
// and assign it to the object -> it is called "object creation"
// the function is only assigning the properties to the object (not creating the object)
// "val pl =" is for storing the location of the object
// Parent of p1 -> Object
// p1 -> product.prototype -> Object.prototype -> null
// Object is not a class. It is a function which is used to create objects
// Object.prototype -> has properties and methods -> the actual class
// product.prototype -> class created automatically by js
console.log(".......Custom Types.........");
var tool = this;
tool.a = 10;
console.log(tool.a);
console.log(this.a);
console.log(this);
console.log(p1);
// Creating function inside the function vs inside the class
// This will create the getInfo every time an object is created -> bad way
function Product(name, brand) {
this.name = name;
this.brand = brand;
this.getInfo = function () {
return this.name + " " + this.brand;
}
}
// this will create the getInfo only once -> good way
function Product(name, brand) {
this.name = name;
this.brand = brand;
}
Product.prototype.getInfo = function () {
return this.name + " " + this.brand;
}
// We are putting the function in the class itself -> good way
var p2 = new Product();
p2.name = "Iphone";
p2.brand = "Apple";
console.log(p2.getInfo());
// JS use prototypical inheritance not class inheritance
// Prototypical inheritance is a runtime inheritance
// Class inheritance is a compile time inheritance
// JS does not have private variables for objects
// For creating private variables -> use closures
function Product(name, brand) {
this.name = name;
this.brand = brand;
var price = 100;
this.getprice = function () {
return price;
}
}
// Now we can access the price using this.getprice() but we cannot change the price using this.price = 200;
p1.city = "Delhi";
delete p2.brand;
// This is the benifit of javascript
// We can add and delete properties of different objects of the same class
// We can also prevent extending of the class
Object.preventExtensions(p1); // p1 cannot be extended
Object.seal(p1); // p1 cannot be extended or deleted
Object.freeze(p1); // p1 cannot be extended, modified or deleted
// Object.seal makes the object like an object oriented class object
console.log(".......preventExtensions, seal, freeze.........");
if (Object.isExtensible(p1)) {
console.log("Extensible");
} else {
console.log("Not Extensible");
}
if (Object.isSealed(p1)) {
console.log("Sealed");
} else {
console.log("Not Sealed");
}
if (Object.isFrozen(p1)) {
console.log("Frozen");
} else {
console.log("Not Frozen");
}
if ("brand" in p1) {
console.log("brand is in p1");
} else {
console.log("brand is not in p1");
}
if ("city" in p1) {
console.log("city is in p1");
} else {
console.log("city is not in p1");
}