0

In a simulation I'm making, I'll have an Element class, which I'll call with params about the properties of that Element (melting and boiling temperatures, rules for reactions with other Elements, color, density, etc.) to create the basic types of Elements (water, oxygen, carbon, etc.). From these, I simply would like to create new "Atoms" from the water, oxygen, and carbon templates I would have. I'm wondering if there's a way to use a constructor (Element) to create a new constructor (like Water)? For example, I would like to be able to do something like this.

var Element = function(/* params */) {
    // common element properties and basic functions
}
var Water = new Element(); // create the new element
var WaterAtom = new Water(); // create the atom (an instance of the element)
// draw the atom, manipulate it, react with other atoms, etc.

I'm basically asking, can a constructor create another constructor? I would like it this way so I don't have to create tons and tons of .prototype code that extends the basic Element class.

8
  • What is this supposed to accomplish? Where do you specify new behavior for the Water class? Commented Feb 18, 2016 at 0:57
  • No, a constructor can't create another constructor, you should consider it to be a simple constructor-producing function that is not invoked with new. Commented Feb 18, 2016 at 1:11
  • Are these things supposed to be subclasses of Element? Then Element is a constructor that constructs element instances - not constructors. Commented Feb 18, 2016 at 1:12
  • Btw, I think you mean Hydrogen not Water. Commented Feb 18, 2016 at 1:12
  • Aren't you simply asking about JavaScript inheritance here? Commented Feb 18, 2016 at 1:35

2 Answers 2

1

I think what you are looking for is

function Element(…) {
    // init properties that all elements share
}
Element.prototype.… = function(…) { … }; // add all methods of elements

Element.makeType = function(rules) {
    function ElementType(…) {
        Element.call(this, …);
    }
    ElementType.prototype = Object.create(Element.prototype);
    ElementType.prototype.constructor = ElementType;
    ElementType.prototype.rules = rules;
    return ElementType;
};

so that you can use it like

var Water = Element.makeType(function(…) {
    // do whatever makes water special
});

var drop = new Water(…);
Sign up to request clarification or add additional context in comments.

1 Comment

Nice! I'll try it out.
1

You could just write a utility function for generating subclasses of Element:

function Element() {}

function subElement() {
  function SubElement() {}
  SubElement.prototype = Object.create(Element.prototype);
  return SubElement;
}

var Water = subElement();
var waterAtom = new Water();

3 Comments

Object.create(Element); - nope.
@SimpleJ Hmm... I'll try it.
@Bergi Woops. I guess I've become too use to the class syntax

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.