Posts

Showing posts from May, 2017

Variables and scoping in ECMAScript 6

http://2ality.com/2015/02/es6-scoping.html This blog post examines how variables and scoping are handled in ECMAScript 6  [1] . Block scoping via  let  and  const    Both  let  and  const  create variables that are  block-scoped  – they only exist within the innermost block that surrounds them. The following code demonstrates that the  let -declared variable  tmp  only exists inside the then-block of the  if  statement: function func ( ) { if ( true ) { let tmp = 123 ; } console .log(tmp); // ReferenceError: tmp is not defined } In contrast,  var -declared variables are function-scoped: function func ( ) { if ( true ) { var tmp = 123 ; } console .log(tmp); // 123 } Block scoping means that you can shadow variables within a function: function func ( ) { let foo = 5 ; if (···) { let foo = 10 ; // shadows outer `foo` cons...

Why Enum Singleton are better in Java

Enum Singletons  are new way to implement Singleton pattern in Java by using Enum with just one instance. Though Singleton pattern in Java exists from long time Enum Singletons are relatively new concept and in practice from Java 5 onwards after introduction of Enum as keyword and feature. This article is somewhat related to my earlier post on Singleton,  10 interview questions on Singleton pattern in Java  where we have discussed common questions asked on interviews about Singleton pattern and  10 Java enum examples , where we have seen how versatile enum can be. This post is about  why should we use Enum as Singleton in Java , What benefit it offers compared to conventional singleton methods etc. 1) Enum Singletons are easy to write This is by far biggest advantage, if you have been writing Singletons prior ot Java 5 than you know that even with double checked locking you can have more than one instances. though that issue is fixed with Java memory model i...