I am tasked with refactoring some code from a former employee. He uses a constructor function that has two things that seem strange – 1, arbitrary code that runs outside of a function or property, and 2, locally defined functions. This is a simplification of what he is doing:
var Dog = function(){
// Arbitrary code
console.log('I am a dog');
var foo = 'foo';
// Function defined to local variable
var bar = function(){
console.log(foo);
console.log('bar');
};
// Normal function in a constructor
this.bark = function(){
console.log('bark');
};
};
var d = new Dog();
Is there any merit to this style of constructor function? Or would it be better to refactor it to only define functions using this
in the style of bark
, and then running them on d
as needed?