Some Web Development Key Points.

1. when we use a script tag to load any JavaScript code, the HTML parsing is paused by the browser when it encounters the script tag and it starts to download the JavaScript file first. The HTML elements script tag will not be executed until the browser is done with downloading the script and executing it. The browser waits till the script gets downloaded, executed and after this, it processes the rest of the page.

Solution: Use script and style tags after body tag.

2. An asynchronous function is implemented using async, await and promises.

2.1. Synchronous JavaScript: As the name suggests synchronous means to be in a sequence, i.e. every statement of the code gets executed one by one. So, basically a statement has to wait for the earlier statement to get executed. 

2.2. Asynchronous JavaScript: Asynchronous code allows the program to be executed immediately where the synchronous code will block further execution of the remaining code until it finishes the current one. This may not look like a big problem but when you see it in a bigger picture you realize that it may lead to delaying the User Interface.

3. Token based auth -JWT
Comparing with Session-based Authentication that need to store Session on Cookie, the big advantage of Token-based Authentication is that we store the JSON Web Token (JWT) on Client side: Local Storage for Browser, Keychain for IOS and SharedPreferences for Android.

There are three important parts of a JWT: Header, Payload, Signature. Together they are combined to a standard structure: header.payload.signature.

The Client typically attaches JWT in Authorization header with Bearer prefix:

Authorization: Bearer [header].[payload].[signature]

Or only in x-access-token header:

x-access-token: [header].[payload].[signature]
The process of converting an object to a file is also known as Serialization or Marshalling or Flattening

Iterating Through Objects

The most common way to loop through properties in an object is with a for…in loop:

for (var key in myObject) {
  console.log(key); // logs keys in myObject
  console.log(myObject[key]); // logs values in myObject
}

Another way to iterate through object properties is by appending the forEach() method to Object.keys():

Object.keys(myObject).forEach(function(key) {
  console.log(key); // logs keys in myObject
  console.log(myObject[key]); // logs values in myObject
});

Ref: JavaScript Immediately-invoked Function Expressions (IIFE) (flaviocopes.com)

An Immediately-invoked Function Expression is a way to execute functions immediately, as soon as they are created. IIFEs are very useful because they don't pollute the global object, and they are a simple way to isolate variables declarations

This is the syntax that defines an IIFE:

(function() {
  /* */
})()

IIFEs can be defined with arrow functions as well:

(() => {
  /* */
})()

We basically have a function defined inside parentheses, and then we append () to execute that function: (/* function */)().

Those wrapping parentheses are actually what make our function, internally, be considered an expression. Otherwise, the function declaration would be invalid, because we didn’t specify any name:

Invalid function declaration

Function declarations want a name, while function expressions do not require it.

You could also put the invoking parentheses inside the expression parentheses, there is no difference, just a styling preference:

(function() {
  /* */
}())

(() => {
  /* */
}())

Alternative syntax using unary operators

There is some weirder syntax that you can use to create an IIFE, but it’s very rarely used in the real world, and it relies on using any unary operator:

-(function() {
  /* */
})() +
  (function() {
    /* */
  })()

~(function() {
  /* */
})()

!(function() {
  /* */
})()

(does not work with arrow functions)

Named IIFE

An IIFE can also be named regular functions (not arrow functions). This does not change the fact that the function does not “leak” to the global scope, and it cannot be invoked again after its execution:

(function doSomething() {
  /* */
})()

IIFEs starting with a semicolon

You might see this in the wild:

;(function() {
  /* */
})()

This prevents issues when blindly concatenating two JavaScript files. Since JavaScript does not require semicolons, you might concatenate with a file with some statements in its last line that causes a syntax error.

This problem is essentially solved with “smart” code bundlers like webpack.

Mastering the Module Pattern - Ultimate Courses™

Introduction to Webpack (flaviocopes.com)

JavaScript Scope and Closures - William Vincent (wsvincent.com)

Comments

Popular Posts