1

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?

5
  • Yes, it's OK. But please don't use alert but console.log Commented Jun 15, 2015 at 12:44
  • Why would it be not allowed to call functions? If you are making an assumption you should have a base for it. Functions can be called everywhere where expression is allowed. Commented Jun 15, 2015 at 12:46
  • Thanks! And the alert was just a quick example. Commented Jun 15, 2015 at 12:48
  • @Andrey While the example shows a loose function, the title asks about method functions, which do have some differences when called from within a constructor. Commented Jun 15, 2015 at 12:51
  • @ssube then it makes completely different question Commented Jun 15, 2015 at 13:15

3 Answers 3

3

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.

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

Comments

1

Yes, indeed. A "constructor" is actually an initialization routine, called after the object has been physically allocated in memory in order to set up the initial values. You can, therefore, call other methods within it, but I would generally caution against such a design.

What I customarily do is to define another method, say, init(), which I am obliged to call against a new object, in my own code, after having instantiated it. (I have to remember to do that, everywhere, in my application.) The constructor does basic, even generic, setup, then my init routine does more.

Since, IIRC, JavaScript really doesn't have "destructors," I also often define a free() routine which, once again, I am obliged to call on an object before allowing it to disappear into the gloom, eventually to be reaped.

It's a self-imposed discipline that seems to work well for me.

Comments

0

Yes.

It seems that your main worry is becoming dependent on the function? In the case of alert, this fear is probably unwarranted, because alert is native to most implementations of javascript. Given that, here is a silly example of injecting your function, which allows for uncoupling of your constructor and the functions it depends on:

    function alertSomething (message){
    alert(message);
} 

function logSomething (message){
    console.log(message);
}

//inject the function into your constructor instead
function Animal(name, messageDeliverySystem){
    this.name = name;
    this.deliverMessage = messageDeliverySystem;
}

var animalWhoLikesToLog = new Animal("Bob the logging bear", logSomething);
var animalWhoPrefersAlert = new Animal("Franky the alert happy Seal", alertSomething);

animalWhoLikesToLog.deliverMessage("Hi, I am Bob");
animalWhoPrefersAlert.deliverMessage("Hi, Franky here!");

Like I said, this is silly because console.log and alert are in most cases native, but it helps you get the idea.

Comments

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.