0

I've been doing a course about clean code. The course states that being "stringly" typed is a bad thing to readability and recommends using a different structure(The course uses C#):

//Dirty
if (employeeType == "manager") 

//Clean
if (employee.Type == EmployeeType.Manager)

My question is: How can I implement a structure like that in javascript?

Should I create an object like this one?

EmployeeType = {
    Manager: "manager"
}

employee = {
    Type: : "manager"
}

Is this the better way to do it?

Goma_BR
  • 142
  • 4
  • js is loose typing, you should use `employeeType === "manager"` instead of `employeeType == "manager"` (REF: https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons) – Dalin Huang May 31 '17 at 02:22
  • for js loose typing, take a look at this one: http://blog.jeremymartin.name/2008/03/understanding-loose-typing-in.html – Dalin Huang May 31 '17 at 02:23
  • 2
    not sure how "loose typing" is at all relevant! – Jaromanda X May 31 '17 at 02:23

1 Answers1

2

If you use ES6 and classes, you can use instanceof.

class Animal {
    greet() {
      // Do nothing.
    }
}

class Dog extends Animal {
  greet() {
    console.log("Woof!");
  }
}

class Cat extends Animal {
  greet() {
    console.log("Meow!");
  }
}

let dog = new Dog();

console.log(dog instanceof Animal); // Returns true
console.log(dog instanceof Dog); // Returns true
console.log(dog instanceof Cat); // Returns false
console.log(dog instanceof Object); // Caveat: returns true!

Or in ES5:

function Animal() {
}

Animal.prototype.greet = function() {
  // Do nothing
}

function Dog() {
  Animal.call(this);
}

Dog.prototype = Object.create(Animal.prototype);

Dog.prototype.greet = function() {
  console.log("Woof!");
}

function Cat() {
  Animal.call(this);
}

Cat.prototype = Object.create(Animal.prototype);

Cat.prototype.greet = function() {
  console.log("Meow!");
}

var dog = new Dog();

console.log(dog instanceof Animal); // Returns true
console.log(dog instanceof Dog); // Returns true
console.log(dog instanceof Cat); // Returns false
console.log(dog instanceof Object); // Caveat: returns true!

Note: instanceof is not an ES6 feature, but classes are. You can use instanceof with ES5-style prototypes. For more info, see MDN

Alvin Teh
  • 778
  • 1
  • 10
  • 17
  • 1
    Wouldn't the ES5 equivalent involve creating the `greet()` methods on the `.prorotype` rather than directly on each instance? In any case, this doesn't answer the question, which asked how to tell whether an employee is a manager or not, because the OO way to represent that would be instances of `Employee` with a `type` property, or to have a `Manager` that inherits from `Employee`. – nnnnnn May 31 '17 at 02:54
  • @nnnnnn You're right on the ES5 prototype; I have updated that. Didn't see the need to involve inheritance (was just thinking about showing how the OP can use `instanceof` instead of "stringly" types), but have updated the answer to include that so it's more analogous to the question. – Alvin Teh May 31 '17 at 03:34