Singleton Pattern in ES6 and ES7

export default class Singleton {

  static instance;

      return instance;

    this.state = "duke";
    this.instance = this;



let first = new Singleton();
let second = new Singleton();

//output: true

The code above uses ECMAScript 2015/ES6 (classes) and ECMAScript 2016/ES7 (static fields) features. It transcompiled, "old school" ES5 representation looks like:

	function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

	var Singleton = function Singleton() {
	  _classCallCheck(this, Singleton);

	  if (instance) {
	    return instance;

	  this.state = "duke";
	  this.instance = this;

	exports.default = Singleton;

See you at JavaScript Bootstrap or Building Apps with React!

NEW: Online Workhop Effective WebApps without Frameworks is also coming to: MUC Airport.

Airport MUC workshops: Web (SPA, PWAs, Offline, Desktop, Mobile) Applications Essentials and Effective Web Applications. No migrations. #usetheplatform

Podcast: and newsletter:

A book about rethinking Java EE Patterns


Already added
$npm install --save babel-preset-es2016 and
.babelrc has

"presets": ["es2016"]

But still 'Missing class properties transform.'

Posted by Sebastian Cheung on September 13, 2016 at 11:15 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments