Skip to main content
added 1 character in body
Source Link
Greg Burghardt
  • 46.6k
  • 8
  • 87
  • 151

A constructor is meant to initialize an object — set its initial state. Re-calling a constructor is not possible in languages that allow the programmer to mark an instance variable as readonly, final, or const.

And then there is JavaScript.

With JavaScript you can call a constructor after the object has been initialized. The call method of a Function object allows you to pass an object as the first argument, which is used as this inside the function call.

let person = new Person("John", "Doe") Person.call(person, "Jane", "Doe") 

You can do this in JavaScript because it does not use true classes (and a class in ECMA Script is not a true class either), nor is JavaScript a strongly typed language. It uses prototypal inheritance, which means new objects are created from existing objects, not classes. A constructor in JavaScript borders on being just another function, but is invoked at a specific point in an object's life.

That being said, your problem seems to be related to code repetition, not calling a constructor. Fortunately we have lots of options for code reuse. Consider adding a public method that does the reinitializing, and call that method from the constructor. Clearly you have a use case where reusing an entire object is desirable, so you probably do not need to worry about readonly fields.

So just make it easy on yourself. DRY up your code by putting the initialization logic in a public method and have your constructor call it.

A constructor is meant to initialize an object — set its initial state. Re-calling a constructor is not possible in languages that allow the programmer to mark an instance variable as readonly final, or const.

And then there is JavaScript.

With JavaScript you can call a constructor after the object has been initialized. The call method of a Function object allows you to pass an object as the first argument, which is used as this inside the function call.

let person = new Person("John", "Doe") Person.call(person, "Jane", "Doe") 

You can do this in JavaScript because it does not use true classes (and a class in ECMA Script is not a true class either), nor is JavaScript a strongly typed language. It uses prototypal inheritance, which means new objects are created from existing objects, not classes. A constructor in JavaScript borders on being just another function, but is invoked at a specific point in an object's life.

That being said, your problem seems to be related to code repetition, not calling a constructor. Fortunately we have lots of options for code reuse. Consider adding a public method that does the reinitializing, and call that method from the constructor. Clearly you have a use case where reusing an entire object is desirable, so you probably do not need to worry about readonly fields.

So just make it easy on yourself. DRY up your code by putting the initialization logic in a public method and have your constructor call it.

A constructor is meant to initialize an object — set its initial state. Re-calling a constructor is not possible in languages that allow the programmer to mark an instance variable as readonly, final, or const.

And then there is JavaScript.

With JavaScript you can call a constructor after the object has been initialized. The call method of a Function object allows you to pass an object as the first argument, which is used as this inside the function call.

let person = new Person("John", "Doe") Person.call(person, "Jane", "Doe") 

You can do this in JavaScript because it does not use true classes (and a class in ECMA Script is not a true class either), nor is JavaScript a strongly typed language. It uses prototypal inheritance, which means new objects are created from existing objects, not classes. A constructor in JavaScript borders on being just another function, but is invoked at a specific point in an object's life.

That being said, your problem seems to be related to code repetition, not calling a constructor. Fortunately we have lots of options for code reuse. Consider adding a public method that does the reinitializing, and call that method from the constructor. Clearly you have a use case where reusing an entire object is desirable, so you probably do not need to worry about readonly fields.

So just make it easy on yourself. DRY up your code by putting the initialization logic in a public method and have your constructor call it.

added JavaScript example
Source Link
Greg Burghardt
  • 46.6k
  • 8
  • 87
  • 151

A constructor is meant to initialize an object — set its initial state. Re-calling a constructor is not possible in languages that allow the programmer to mark an instance variable as readonly final, or const.

YourAnd then there is JavaScript.

With JavaScript you can call a constructor after the object has been initialized. The call method of a Function object allows you to pass an object as the first argument, which is used as this inside the function call.

let person = new Person("John", "Doe") Person.call(person, "Jane", "Doe") 

You can do this in JavaScript because it does not use true classes (and a class in ECMA Script is not a true class either), nor is JavaScript a strongly typed language. It uses prototypal inheritance, which means new objects are created from existing objects, not classes. A constructor in JavaScript borders on being just another function, but is invoked at a specific point in an object's life.

That being said, your problem seems to be related to code repetition, not calling a constructor. Fortunately we have lots of options for code reuse. Consider adding a public method that does the reinitializing, and call that method from the constructor. Clearly you have a use case where reusing an entire object is desirable, so you probably do not need to worry about readonly fields.

So just make it easy on yourself. DRY up your code by putting the initialization logic in a public method and have your constructor call it.

A constructor is meant to initialize an object — set its initial state. Re-calling a constructor is not possible in languages that allow the programmer to mark an instance variable as readonly final, or const.

Your problem seems to be related to code repetition, not calling a constructor. Fortunately we have lots of options for code reuse. Consider adding a public method that does the reinitializing, and call that method from the constructor. Clearly you have a use case where reusing an entire object is desirable, so you probably do not need to worry about readonly fields.

So just make it easy on yourself. DRY up your code by putting the initialization logic in a public method and have your constructor call it.

A constructor is meant to initialize an object — set its initial state. Re-calling a constructor is not possible in languages that allow the programmer to mark an instance variable as readonly final, or const.

And then there is JavaScript.

With JavaScript you can call a constructor after the object has been initialized. The call method of a Function object allows you to pass an object as the first argument, which is used as this inside the function call.

let person = new Person("John", "Doe") Person.call(person, "Jane", "Doe") 

You can do this in JavaScript because it does not use true classes (and a class in ECMA Script is not a true class either), nor is JavaScript a strongly typed language. It uses prototypal inheritance, which means new objects are created from existing objects, not classes. A constructor in JavaScript borders on being just another function, but is invoked at a specific point in an object's life.

That being said, your problem seems to be related to code repetition, not calling a constructor. Fortunately we have lots of options for code reuse. Consider adding a public method that does the reinitializing, and call that method from the constructor. Clearly you have a use case where reusing an entire object is desirable, so you probably do not need to worry about readonly fields.

So just make it easy on yourself. DRY up your code by putting the initialization logic in a public method and have your constructor call it.

Source Link
Greg Burghardt
  • 46.6k
  • 8
  • 87
  • 151

A constructor is meant to initialize an object — set its initial state. Re-calling a constructor is not possible in languages that allow the programmer to mark an instance variable as readonly final, or const.

Your problem seems to be related to code repetition, not calling a constructor. Fortunately we have lots of options for code reuse. Consider adding a public method that does the reinitializing, and call that method from the constructor. Clearly you have a use case where reusing an entire object is desirable, so you probably do not need to worry about readonly fields.

So just make it easy on yourself. DRY up your code by putting the initialization logic in a public method and have your constructor call it.