Showing posts from October, 2019

Disallow specific imports in Javascript Project

Have you been in   scenario  where team members are using different libraries to perform identical functionalities and resulting in a large JS  bundle? Well, recently our team faced the same scenario. We started a new project related to the  Ethereum block chain. To interact with the Ethereum Block Chain and its ecosystem there are couple of libraries available in market from which  ethers.js  and  web3js  are widely used. In our project we decided to use the former one. After the couple of days an inter joins us. We gave her a small task to start with,  so at the end of the day, she imported the web3.js performed the task and committed the changes !!!.  So here we have unnecessary import in project that will bloat our JS bundle o r there might  some scenario in which we may want to restrict import of a sub module or part of a large module. Well in such case ESLint  's  no-restricted-imports  rule can rescue us. Below is simple example of using  no-restricted-import  by de

Different methods to copy an object in JavaScript

In this post we will discuss the different methods to copy the javascript object and discuss the outcome of each method. But before that just keep one thing in mind that javascript objects are  mutable   by nature and stored as a reference. So when you assign the object to another variable, you're just assigning the memory address of the object to that variable. In this case your old and new object both will point to the same memory address. Any change in either object will be reflected in the other. So just assigning the one object to another will not really copies your object!!! // Define object bar var bar = { x: 'Hi' } console. log ( bar .x); //Hi // Assign it to foo var foo= bar ; console. log (foo.x); //Hi //But foo.x='Hello!! Im foo.'; console. log (foo.x); //Hello!! Im foo. console. log ( bar .x); //Hello!! Im foo. bar .x= "Nice to meet you foo!!" ; console. log (foo.x); //Nice to meet you foo!! console. log ( bar .x);