Skip to main content

Posts

Showing posts with the label javascript

JavaScript Series (03) — handle the undefined!

The beauty of javascript is, that it allows you to dynamically add behaviors and states in an object  at runtime . So an object can have more properties than what was mentioned during the declaration. It even allows you to dynamically update an object's  prototype chain.  So all other objects of the same type get access to these updated behaviors or states. The Problem To support this dynamic addition feature, Javascript allows us to write code that accesses  any  random property on an object with a ( . ) operator. It does not verify if that property should be accessed or not. For example, If you have a  Vehicle , which can be any valid vehicle, like a car, airplane, bike, ship, etc. then  in a badly designed codebase,  you can encounter the below code. let flightDuration = vehicle.flightDetails.duration Let's assume the vehicle object we are dealing with, is a Car, In 2022 it's fair to assume that a car can not fly. For a car the expression...

JavaScript Series (02) — Introducing ES6

The  ECMAScript 2015  aka  ES6 , is the 6th edition of the  ECMAScript  language specification standards. What ES6 did to Javascript was what Java8 did to Java. ES6 came with many new features to enhance the existing language and many new concepts to write clean and concise code. Here are some additions in ES6 1. let & const (Block Scope without Hoisting ) If you’ve coded in Javascript before 2016, you know how vulnerable the  var  keyword made the applications. Using it outside a function was a suicide but even within a function, it created problems due to hoisting. Now, what was hoisting? Hoisting  is  a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution . So no matter where you declare  var , it will always be moved to the top of its enclosing function. So any block within the function will still see the same variable. This doesn’t leave any space to have scopes...