JavaScript - Summary Of JavaScript Lessons

 


      Introduction

      Classes, objects and prototype

      How ‘this’ acts

      Strict notation

      Function closure

      Callbacks and promises

 

JavaScript

By default, JavaScript programs run using a single thread. Though there are ways to create new threads JavaScript is considered a Single-threaded language. JavaScript does not wait for I/O operations to get completed, instead, it continues the execution of the program. This is called non-blocking I/O. JavaScript is asynchronous because of the NIO nature. JavaScript is dynamically typed. It determines variable types, ordering, etc. in runtime. JavaScript supports OOP as well as functional programming (Multi-paradigm). JavaScript has an eventing system that manages its asynchronous operations.

Classes and objects

      In JavaScript, a constructor function is used with the ‘new’ key keyword when creating a Object.

      Constructor the function is just another function.

      When function is used with the ‘new’ keyword that function acts as a Class.

      Recently JavaScript introduced the ‘class’ keyword, but it is not yet adopted by all JavaScript engines.

      Another way of creating an object is using object literals (‘{}’). These objects are considered to be a singleton.

      JavaScript supports static methods and variables.

      When ‘new’ the keyword is used, a new object is created and it assigned as ‘this’ for the duration of the call to the constructor function. 

Prototypes

      JavaScript functions as a reference to another object called a prototype. It is somewhat similar to the class definition in other languages.

      It is really another object instance.

      In JavaScript prototype object is used when creating objects, for inheritance and adding methods to a JavaScript class.

      Because of the flexibility in JavaScript there are multiple ways to create classes as well as to extend classes. Prototypes are the recommended way of doing so.

      Function that is being used to create objects is called a constructor function.

      Object the instance also has a prototype it is basically the object instance from which object is being created. Object ‘__proto__’ is where the object gets its properties inherited from.

      Functions the prototype is used to inherit properties to object instances.

‘this’ in JavaScript

      Unlikely other languages in JavaScript ‘this’ keyword acts differently.

      Inside a object ‘this’ refers to the object itself.

      In global context ‘this’ refers to the global object (in the browser it is the window object). This behavior will get changed in strict mode.

      If a function which is using ‘this’ keyword is being passed to another object then ‘this’ will refer to that object, but not to the original object where the function was declared in the first place.

      This behavior is very noticeable in callback and closures.

Strict notation

      Restricted mode of JavaScript.

      Purpose it makes it easier to write secure JavaScript.

      Strict mode make bad practices in JavaScript to errors.

      Keep developer away from using syntaxes that will get invalidate with future JavaScript developments.

      For example, it does not allow creating variables without the var keyword (Variable have to be declared).

      Another example would be it will stop referring to the window object as ‘this’ from outside object instances.

Closure

      JavaScript the closure is a function that returns another function.

      In JavaScript, the closure is used to encapsulate variables into a function and restrict access to it from the outside.

      JavaScript creates an environment with all the local variables from the outer function when the inner function is created. The closure is the combination of this environment and the inner function. 

Callback and promises

      JavaScript is asynchronous. All I/O operations in JavaScript are implemented to be asynchronous by nature.

      Reason for this is JavaScript being a single-threaded language if an I/O operation holds the thread till it gets completed JavaScript won’t perform well as a programming language.

      But asynchronous operation introduce difficulty when we need to do synchronous processing user data.

      This is solved by using callbacks and promises.

      The call back is a function that is being passed to an async task and on completion the function will be executed.

   The promise is an object that is being returned from async tasks. The promise has properties to deal with async operations synchronously.

      Nested callbacks passed into the sequence of async tasks are referred to as a ‘callback hell’.

      Promise object was introduced to solve this problem.

      Promise object has a set of properties, methods, and mechanism of chaining to handle complex async tasks nicely.


Comments

Popular posts from this blog

Machine learning and artificial intelligence

SALESFORCE

Test Tools