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?

3
  • js is loose typing, you should use employeeType === "manager" instead of employeeType == "manager" (REF: stackoverflow.com/questions/359494/…) Commented May 31, 2017 at 2:22
  • for js loose typing, take a look at this one: blog.jeremymartin.name/2008/03/… Commented May 31, 2017 at 2:23
  • 2
    not sure how "loose typing" is at all relevant! Commented May 31, 2017 at 2:23

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.