Posts

What are Observables?

According to the definition, Observables defined as lazy Push collections of multiple values. Don't confuse, Now just bear with me, soon all your doubts will be gone. Before Diving into understanding the Observables, lets understand two strategies or protocols PUSH & PUll used in the programming languages. Now when you write some code, it means some code is producing some data & other code is going to consume it. So in simple terms we may have a Data-Producer, which produce the data and Data-Consumer who is just going to consume it. So let's take an example to understand Producer and Consumer. Let suppose we have a function sum & som code is calling it. // producer   function foo() {   return 1;   }   // consumer   const val = foo();   console.log(val); Here, you can simply say that function foo is producing data, So you can consider it as a Producer. And the below code is calling foo to consume the data, So it you can consider it as a Consumer. Now,

What is MQTT protocol & its use

Image
So let' start journey towards gets basics about MQTT. Before understanding MQTT, we should know what basically is publish/subscribe design pattern. The publish/subscribe pattern (also known as pub/sub ) provides an alternative to traditional client-server architecture.  In the client-sever model , a client communicates directly with an endpoint. The pub/sub model decouples the client that sends a message (the publisher) from the client or clients that receive the messages (the subscribers). The publishers and subscribers never contact each other directly. In fact, they are not even aware that the other exists.  The connection between them is handled by a third component (the Broker - eg https://mosquitto.org ). The job of the Broker is to filter all incoming messages and distribute them correctly to subscribers. Now coming back to MQTT - MQTT is a Client Server publish/subscribe messaging transport protocol. Being a light weight, open, simple protocol

What the hack is Call, bind & apply

As Javascript developers, most of the time we are pretty confused about the Call, bind & apply & their use in JavaScript. So let's try to understand. 1. CALL & APPLY Both are these methods are simply known as immediately invoked function. What basically it means whenever you defined both these methods on a function it execute immediately as we run the program. Technically, if we define CALL & APPLY, both are used to execute a function in a different this context. Where this is passed as first argument & the second or subsequent argument (for Call ) is treated as arguments Only difference b/w them is that CALL takes argument as comma separated while apply takes argument as an array with this as first argument. So syntax wise, we can simply write them as follows - f.call(<this-context>, a1, a2, a3, ...) f.apply(<this-context>, [a1, a2, a3, ...]) Where f is a function to be called in respect of a different this context, where in the

What is Provider in Angular Js?

What is Provider in Angular Js? You can define Provider as service which is configurable and which should be used when you want to expose an API for application-wide configuration that must be made before the application starts. So technically speaking, it make sense during bootstrap(config) phase. During application bootstrap, before AngularJS goes off creating all services, it configures and instantiates all providers. We call this the configuration phase of the application life-cycle . During this phase, services aren't accessible because they haven't been created yet. Once the configuration phase is over, interaction with providers is disallowed and the process of creating services starts. We call this part of the application life-cycle the run phase . So simply speaking there is no way to use any service in config phase except constant & Provider. Technically, a Provider is defined as an object with a $get function. Example - So let s

What is Closure ?

Image
A closure is an inner function that has access to the variables in the outer (enclosing) function’s scope chain. The closure has access to variables in three scopes; specifically: (1) variable in its own scope, (2) variables in the enclosing function’s scope, and (3) global variables. Here is an example : var globalVar = "xyz"; (function outerFunc(outerArg) { var outerVar = 'a'; (function innerFunc(innerArg) { var innerVar = 'b'; console.log( "outerArg = " + outerArg + "\n" + "innerArg = " + innerArg + "\n" + "outerVar = " + outerVar + "\n" + "innerVar = " + innerVar + "\n" + "globalVar = " + globalVar); })(456); })(123); In the above example, variables from innerFunc , outerFunc , and the global namespace are all in scope in the innerFunc . The above code will therefore produce the following output: out

What is IIFE in Javascript ?

An  IIFE  is an anonymous function that is created and then immediately invoked. It's not called from anywhere else (hence why it's anonymous), but runs just after being created. It has nothing to do with any event-handler for any events (such as  document.onload ). The first pair of parentheses  ( function(){...} )  turns the code within (in this case, a function) into an expression, and the second pair of parentheses  (function(){...}) ()  calls the function that results from that evaluated expression. This pattern is often used when trying to avoid polluting the global namespace, because all the variables used inside the IIFE (like in any other  normal  function) are not visible outside its scope. ( function (){ // all your code here // ... })(); // foo is unreachable here (it’s undefined)

Defining Constant in Javascript

Prior to javascript ES6, there was no way to define constants, so one have to write their own custom way to write constants. So, here are two ways to define constant that are mutable i.e, which can'nt be reassigned. 1. Using Const (ES6) : - const MAX_NUM = 10; This  const  means that you can't reassign it to any other value. Check the  compatibility notes  to see if your targeted browsers are supported. 2. Using defineProperty And we can use throughout our application. (function() {     var locarVar;     Object.defineProperty(window, ' MAX_NUM ', {         get: function() {             return locarVar;         },         set: function(val) {             locarVar = window. MAX_NUM  || val;         }     }); }()); MAX_NUM = 10; MAX_NUM = 12; //  Cannot redefine property: MAX_NUM console.log(MAX_NUM); // 10