JSFiddle - React, Tailwind, and code Playground

by ifightcrime

JavaScript

var myObject = {
  init: function(thing) {
    if (thing) this.something = 'omg';
  }
};

myObject.init(true);

// outputs 'omg'
console.log(myObject.something);

myObject.init();

// still outputs 'omg', which could be unexpected the second time around
console.log(myObject.something);

var myClass = function(thing) {
  if (thing) this.something = 'omg';
}

var instance = new myClass(true);

// outputs 'omg'
console.log(instance.something);

var another_instance_later = new myClass();

// this time it correctly outputs 'undefined'
console.log(another_instance_later.something);