You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Javascript/closure.md
+5-4Lines changed: 5 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
-
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.
1
+
A closure is the combination of a function bundled together (enclosed) with **references to its surrounding state (the lexical environment)**. In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.
2
2
3
-
A closure is a function that retains access to variables of the context it was created in even after said function call has returned.
3
+
**A closure is a function** that retains access to variables of the context it was created in even after said function call has returned.
Among other things, closures are commonly used to give objects data privacy. Data privacy is an essential property that helps us program to an interface, not an implementation. This is an important concept that helps us build more robust software because implementation details are more likely to change in breaking ways than interface contracts.
23
23
24
-
In JavaScript, closures are the primary mechanism used to enable data privacy. When you use closures for data privacy, the enclosed variables are only in scope within the containing (outer) function. You can’t get at the data from an outside scope except through the object’s privileged methods. In JavaScript, any exposed method defined within the closure scope is privileged. For example:
24
+
To use a closure, simply define a function inside another function and expose it. To expose a function, return it or pass it to another function.
25
+
26
+
#### In JavaScript, closures are the primary mechanism used to enable data privacy. When you use closures for data privacy, the enclosed variables are only in scope within the containing (outer) function. You can’t get at the data from an outside scope except through the object’s privileged methods. In JavaScript, any exposed method defined within the closure scope is privileged. For example:
25
27
26
28
```js
27
29
constgetSecret= (secret) => {
28
30
return {
29
31
getPrivileged: () => secret
30
32
}
31
33
}
32
-
33
34
```
34
35
35
36
In the example above, the `.getPrivileged()` method is defined inside the scope of `getSecret()`, which gives it access to any variables from `getSecret()`, and makes it a privileged method. In this case, the parameter, `secret`.
Copy file name to clipboardExpand all lines: Javascript/sequential-execution-of-codes-React-Node-Context-Master-Notes/Async-await-API-call-Simple-Example-synchronous-Fetchl.md
+9-2Lines changed: 9 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
### Transform the following code with
1
+
### Transform the following code with sequential execution. I want to fire second request based on one value returned by the first request.
2
2
3
3
```js
4
4
fetch("https://api.com/values/1")
@@ -18,4 +18,11 @@ const request = async () => {
18
18
request();
19
19
```
20
20
21
-
In the first line, we make a GET request to https://api.com/values/1. Instead of continuing to the next line, we wait for the request to finish, hence await. When it finishes, it passes the resolved value to the response variable.
21
+
In the first line, we make a GET request to https://api.com/values/1. Instead of continuing to the next line, we wait for the request to finish, hence await. When it finishes, it passes the resolved value to the *response* variable.
22
+
23
+
In the second line, we get the JSON version of the response. Again, we use await so we can wait for it to complete (or fail) and then pass the result to the json variable.
Copy file name to clipboardExpand all lines: Node-Express/sesstionStorage-vs-localStorage-vs-Cookie.md
+20-6Lines changed: 20 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,9 @@
1
+
**localStorage** and **sessionStorage** both extend Storage. There is no difference between them except for the intended "non-persistence" of sessionStorage.
2
+
3
+
**That is, the data stored in localStorage persists until explicitly deleted. Changes made are saved and available for all current and future visits to the site.**
4
+
5
+
For sessionStorage, changes are only available per tab. Changes made are saved and available for the current page in the that tab until it is closed. Once it is closed, the stored data is deleted.
6
+
1
7
[From Official Dox](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API)
2
8
3
9
The two mechanisms within Web Storage are as follows:
@@ -6,29 +12,37 @@ The two mechanisms within Web Storage are as follows:
6
12
7
13
**localStorage** does the same thing, but persists even when the browser is closed and reopened.
8
14
9
-
These mechanisms are available via the Window.sessionStorage and Window.localStorage properties (to be more precise, in supporting browsers the Window object implements the WindowLocalStorage and WindowSessionStorage objects, which the localStorage and sessionStorage properties hang off) — invoking one of these will create an instance of the Storage object, through which data items can be set, retrieved and removed. A different Storage object is used for the sessionStorage and localStorage for each origin — they function and are controlled separately.
15
+
These mechanisms are available via the **Window.sessionStorage** and **Window.localStorage** properties (to be more precise, in supporting browsers the Window object implements the WindowLocalStorage and WindowSessionStorage objects, which the localStorage and sessionStorage properties hang off) — invoking one of these will create an instance of the Storage object, through which data items can be set, retrieved and removed. A different Storage object is used for the sessionStorage and localStorage for each origin — they function and are controlled separately.
10
16
11
17
localStorage and sessionStorage are perfect for persisting non-sensitive data needed within client scripts between pages (for example: preferences, scores in games). The data stored in localStorage and sessionStorage can easily be read or changed from within the client/browser so should not be relied upon for storage of sensitive or security-related data within applications.
12
18
13
-
#### So sessionStorage (as the name suggests) is only available for the duration of the browser session (and is deleted when the tab or window is closed) - it does, however, survive page reloads (source DOM Storage guide - Mozilla Developer Network). Clearly, if the data you are storing needs to be available on an ongoing basis then localStorage is preferable to sessionStorage. The data stored in localStorage persists until explicitly deleted. Changes made are saved and available for all current and future visits to the site.
19
+
#### sessionStorage (as the name suggests) is only available for the duration of the browser session (and is deleted when the tab or window is closed) - it does, however, survive page reloads (source DOM Storage guide - Mozilla Developer Network). Clearly, if the data you are storing needs to be available on an ongoing basis then localStorage is preferable to sessionStorage. The data stored in localStorage persists until explicitly deleted. Changes made are saved and available for all current and future visits to the site.
14
20
15
21
### LocalStorage - Some more details
16
22
17
23
##### Advantage of localStorage -
24
+
1> Storage limit is the maximum amongst the three
25
+
26
+
2> One of the most important differences is that unlike with cookies, data does not have to be sent back and forth with every HTTP request. This reduces the overall traffic between the client and the server and the amount of wasted bandwidth. This is because data is stored on the user’s local disk and is not destroyed or cleared by the loss of an internet connection.
18
27
19
28
##### Disadvantage of localStorage -
20
29
21
-
1> It works on same-origin policy. So, data stored will only be available on the same origin.
30
+
1> LocalStorage becomes slower as more data is stored4. For most browsers the time it takes for localStorage.getItem to return increases from a few milliseconds with a near empty storage to over a second with a full storage.
31
+
32
+
2> It works on same-origin policy. So, data stored will only be available on the same origin.
33
+
34
+
3> Any JavaScript code on your page can access local storage: it has no data protection whatsoever. This is the big one for security reasons
22
35
23
36
### SessionStorage - Some more details
37
+
1>
24
38
25
39
##### Advantage of sessionStorage -
26
40
27
41
##### Disadvantage of sessionStorage -
28
42
29
43
1> The data is available only inside the window/tab in which it was set.
30
44
31
-
2> Like localStorage, tt works on same-origin policy. So, data stored will only be available on the same origin.
45
+
2> Like localStorage, it works on same-origin policy. So, data stored will only be available on the same origin.
32
46
33
47
### More General Info
34
48
@@ -41,7 +55,7 @@ The maximum storage available is different per browser, but most browsers have i
Always catch LocalStorage security and quota exceeded errors
58
+
**Always catch LocalStorage security and quota exceeded errors**
45
59
46
60
QuotaExceededError: When storage limits exceeds on this function window.sessionStorage.setItem(key, value);, it throws a "QuotaExceededError" DOMException exception if the new value couldn't be set. (Setting could fail if, e.g., the user has disabled storage for the site, or if the quota has been exceeded.)
47
61
@@ -59,5 +73,5 @@ Compared to others, there's no advantages of Cookies
59
73
Stores data that has to be sent back to the server with subsequent requests. Its expiration varies based on the type and the expiration duration can be set from either server-side or client-side (normally from server-side).
60
74
61
75
Cookies are primarily for server-side reading (can also be read on client-side), localStorage and sessionStorage can only be read on client-side.
62
-
Size must be less than 4KB.
76
+
Cookies Size must be less than 4KB.
63
77
Cookies can be made secure by setting the httpOnly flag as true for that cookie. This prevents client-side access to that cookie
0 commit comments