-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL2.js
More file actions
145 lines (117 loc) · 3.56 KB
/
L2.js
File metadata and controls
145 lines (117 loc) · 3.56 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
// Special type -> undefined
function f1(arg1) {
// return nothing
}
var r = f1(10);
console.log(r); // undefined
// Overloading -> same function name but different parameters
function f2(arg1) {
return arg1;
}
function f2(arg1, arg2) {
return arg1 + arg2;
}
var r = f2(10, 20);
console.log(r); // 30
var r = f2(10);
console.log(r); // This will not work
// Only the last defined function will be executed
// This is because the javascript is an interpreted language
// It just overwrites the previous function
// One way is to define the function with maximum number of parameters
function f3(arg1, arg2, arg3) {
if (arguments.length > 2) {
return arg1 + arg2 + arg3;
}
if (arguments.length > 1) {
return arg1 + arg2;
}
if (arguments.length > 0) {
return arg1;
}
return 0;
}
console.log(".......Overloading.........");
console.log(f3(10, 20, 30)); // 60
console.log(f3(10, 20)); // 30
console.log(f3(10)); // 10
console.log(f3()); // 0
// Polymorphism -> same function name but different parameters
// We can pass functions as parameters
function log() {
console.log("log");
}
function trace() {
console.log("trace");
}
function run(arg1, arg2, arg3) {
var sum = arg1 + arg2;
console.log(sum);
arg3();
}
run(10, 20, log); // 30
run(10, 20, trace); // 30
// Async Operation
// Closure function
function f4(arg1, arg2, arg3) {
var localVar = 10;
setTimeout(function () { // closure function
console.log(arg1 + arg2);
arg3();
}, 1000);
}
// Required data will not be garbage collected
// Browser
// UI Rendering Engine JS Engine Browser API (NodeJS API)
// DOM Call Stack | Task/Event Queue setTimeout(cb, ms)
// xhr
// onclick(cb)
// For things like setTimeout, onclick.. -> we need to store the arguments
// Safegaurding the context
function Product(name, brand) {
this.name = name;
this.brand = brand;
this.getInfo = function () {
setTimeout(
function getInfoFromServer() {
this.data = [10, 20, 30]; // here this will not p1 | window.data = [10, 20, 30];
}, 1000);
}
}
// This will not output error but
// the problem is "this" pointer will changing the location
// How to fix this?
// store the location of the object in the variable "self"
function Product(name, brand) {
this.name = name;
this.brand = brand;
this.getInfo = function () {
var self = this; // we will store the location of the object in the variable "self"
setTimeout(
function getInfoFromServer() {
self.data = [10, 20, 30]; // here this will be p1
}, 1000);
}
}
// This is called "safegaurding the context"
var p1 = new Product("Iphone", "Apple");
p1.getInfo();
// wait for 1000 msec before calling console log
console.log(p1.data); // undefined
setTimeout(function () {
console.log(p1.data);
}, 1000);
// Second way:- Safegaurding the context using bind
// We will educate the function that it use the correct context
// We will use "bind" method
function Product(name, brand) {
this.name = name;
this.brand = brand;
function getInfoFromServer() {
this.data = [10, 20, 30];
}
this.getInfo = function () {
setTimeout(getInfoFromServer.bind(this), 1000);
}
}
// Not only "this" we can bind other and multiple arguments/variables