Yang K
Yang K

Reputation: 437

JS: Running methods from the constructor and not using object again

I'm writing a small JS application and wanted to ask about some best practices. So, say I have a class called Dog like this:

class Dog {
    constructor(name) {
        this.name = name;
    }
    bark() {
        console.log(`Bark Bark - ${this.name}`);
    }
    sayName() {
        console.log(`My name is ${this.name}`);
    }
}

When I create a new object that is an instance of the Dog class, I always need to call bark() and sayName(). When doing this, is it advised to call these methods from the constructor like this:

constructor(name) {
    this.name = name;
    this.bark();
    this.sayName();
}

or is it better to call them outside after initializing the object like this:

let germanShepard = new Dog("german shepard");
germanShepard.bark();
germanShepard.sayName();

Note: I never need to use bark() and sayName() after doing this. It's only a one time thing.

So, what do you guys recommend? Are there any advantages for one over the other? Thanks in advance.

Upvotes: 0

Views: 239

Answers (2)

Nikita
Nikita

Reputation: 111

Want to add what Bergi wrote.

If those methods (bark and sayName) are needed handling input parameters, you definitely should use it from constructor. Simple example: you want to store in you class Dog hashed 'name', rather simple 'name'. To do that you will create method hashDogsName, that inputs a name as parameter and returns string.

function hashDogName(name) { return name.split('').reverse().join(''); }

Please, do not write such a thing, I wrote this mess to show, that function returns other string

And in your constructor you will use it:

class Dog {
    constructor(name) {
        this.hashedName = this.hashDogName(name);
    }
}

So you need to use function in constructor, if you need to store edited object. In other situation you'll have to explain the employeer / other devs why you used functions in constructor. I can't see any other cases why you should use class functions in constructor (except for logs).

Upvotes: 0

Bergi
Bergi

Reputation: 664886

Is it advised to call these methods from the constructor

No, don't do that. A constructor should only initialise an object, it should not do anything else. (It might use helper methods to do that). It definitely should not cause output or other side effects. Using new calls only for running a procedure and then throwing away the object are an antipattern.

I would recommend to instead introduce a static method:

class Dog {
    …

    static fastLife(name) {
        const dog = new this(name);
        dog.bark();
        dog.sayName();
        // dog dies
    }
}

Dog.fastLife("german shepard");

… and not using object again

Taken to the extreme, why do you need a class at all? Why do you need an object at all when you hardly use it? Don't go for OOP when all you need is a little function.

function fastLivedDog(name) {
    function bark() {
        console.log(`Bark Bark - ${name}`);
    }
    function sayName() {
        console.log(`My name is ${name}`);
    }
    // in this example, the local functions are called only once
    // and could be trivially inlined…
    bark();
    sayName();
}

fastLivedDog("german shepard");

Upvotes: 1

Related Questions