JavaScript: Key note on basics

The best practices on minute details ignored which cause bugs in code

Posted by Aditya Pratap Singh on June 9, 2015

Part 1

Objects:Except primitive types i.e. Numbers, strings, boolean, null and undefined , everything in JavaScript is object. Numbers, strings and booleans are like objects as they have methods but they are not mutable. Objects in JavaScript are always passed by reference and never copied.

Global Abatement: Create a single global variable to remove pollution of global scope

for in: The for in operator enumerates through all properties of an object including those from prototype chain and there is no guarantee on order of properties

Object.keys: Object.keys is the equivalent of for in except that it iterates oly over property of current object and not the prototype chain

Prototype: Prototype relation is dynamic i.e. updates to prototype are immediately reflected to all child objects. Update the prototype properties carefully.