IIFE constructor

by Artem

JavaScript

'use strict';

let Animal = (function() {

  const Animal = function(firstName) {
  	if (!(this instanceof Animal)) {
    	return new Animal(firstName);
    }
    this.firstName = firstName;
    const legs = 4;
    const greet = () => {
      alert(`Hello, ${firstName}`)
    };
  };

  Animal.prototype.foo = function() {
    return this.firstName;
  }

  return Animal;

}());

const tiger = Animal('TIGER');
const bear = new Animal('BEAR');
console.log(tiger, bear);