Javascript: Is the call to a method / function in a constructor function permitted?

advertisements

I was wondering if its allowed in JavaScript to call functions or methods in the process of constructing objects with a constructor function since its job is just to create objects.

Example:

function Animal(name) {
  this.name = name;

  alert("I am called in the constructor-function. Is it allowed?");
}

var x = new Animal("Bird");

This works fine (it will create the Object and alert the message) but is it ok to do this?


Certainly!

Constructors are just like any other function, except they setup the object. You can call anything you need/want to from them (in certain cases/languages even another constructor of that object)

for debugging purposes in JavaScript, alerts are fine.