The Promise returned from fetch() won’t reject on HTTP error status even if the response is an HTTP 404 or 500. Instead, it will resolve normally (with ok status set to false), and it will only reject on network failure or if anything prevented the request from completing. By default, fetch won’t send or receive any cookies from the server, resulting in unauthenticated requests if the site relies on maintaining a user session (to send cookies, the credentials init option must be set).
JavaScript has been developed rapidly in past few years. Developers are able to accumulate experience through reduplicated work. However, in order to improve the work efficiency and code quality, it is suggested to organize and maintain your codebase in a elegant way - Write your own JS Library.
How
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
(function(window){ /* function scope starts here */
'use strict';
functiondefine_library(){
var LogDecorator = {}; // will be returned as a global variable
var logging = true; // disable/enable logging globally
var logPrefix = "----------------Start---------------------";
var logSuffix = "-----------------End---------------------";
LogDecorator.log = function(option, targetObj){
if(logging === true){
console.log(logPrefix);
console.log(`Option: ${option}`);
console.log('Target Object:');
console.log(targetObj);
console.log(logSuffix);
}
}
return LogDecorator;
}
//define globally if it doesn't already exist
if(typeof(LogDecorator) === 'undefined'){
window.LogDecorator = define_library();
}
else{
console.log("LogDecorator has already been defined.");
}
})(window); /* function scope ends */
Output Sample:
Test Sample
Why
Use Strict can eliminate silent errors by changing them to throw errors. Besides, Strict Mode can help JavaScript engines to perform optimizations. Refer to Strict mode for more details.
Below is one way of using closures to define public functions that can access private functions and variables which is known as module pattern:
1
2
3
4
5
6
(function() {
// properties
// methods
// ...
})(window);
// Self-Executing Anonymous Functions
() is the scope set for this function you are able to create private methods/functions and properties inside local scope. window inside () is used as global scope in browser environment.
Diffie-Hellman Key Exchange (Value marked as red is private and only visible to its owner)
Diffie-Hellman is a very simple algorithm which can be used to establish a shared secret between two parties.
How it works
Robot A use its private number a to generate value A and pass it to Robot B along with value g and p. Value g, p and A are all public. Robot B uses the same formula to generate value B using its private value b then transfers value B to Robot A. After all of that, Robot A and B can get same value R without exposing its private values to others.
Usage of D-H and Concept Behind
During the network communications, data can be eaily exposed to attackers. D-H provides a fantastic solution for that. Using D-H algorithm, users can estalish a secret even through public channels.
The key concept of this algorithm is Trapdoor Function which is widely used in cryptography. In this case, f(x)=g^x mod p is a Trapdoor Function, f(x) can be easily calculated when x is given but the computation of the inverse of f can hardly be done.
Observer pattern (also known as Pub/Sub pattern) can be used to help break down a whole application into more loosely coupled modules.
There would be an object (observer collection) maintains a list of events. Those observers (subscriber) will be automatically notified once the event occurs.
Code Sample:
observer subscribes to an event by the event name. ‘globalBroadcaster’ is an observer collection which stores the events subscribed. ‘handler’ is the action observer will make once the event occurs.
Once the event be published, all the subscribers of this event will be notified. parameters could be injected when event published and it can be used by the subscribers. */
The biggest benifit of using Sub/Pub pattern comes from the concept of decoupling. Observer A doesn’t need to worry about how the event published or who announces the event, ‘A’ will process its handler once be notified.
The disadvantages also come from decoupling modules. It would be hard for one to draw a clearly workflow since some of the actions processed on the fly. It will cause chaos if not implemented properly.
Anyhow, I like the concept of decoupling, especially in group work.