0

How would you define an object in Javascript such that its body would include properties that are also the object you're defining? For example if I was defining a class called Organism - and the definition of any organism included two more organisms called organismA and organismB? Is it like this:

Organism (){

value: value
Organism organismA; //microbe for example
Organism organismB; //bacteria for example

};
3
  • this might be helpful stackoverflow.com/questions/502366/structs-in-javascript Commented Apr 14, 2015 at 4:00
  • 1
    JavaScript does not do explicit typing, so you're going to have to change that example a little. Short answer: you can put as many objects inside objects as you like, and you can bind as many instance properties that are themselves objects in an object constructor. Commented Apr 14, 2015 at 4:01
  • Simply assign the correct values to the properties. Commented Apr 14, 2015 at 4:03

2 Answers 2

1

Javascript is not a typed language.

You cannot enforce the types of an object's properties. If you want that, look at something like Typescript.

You can make "nested objects"

var microbe = getMicrobe();
var bacteria = getBacteria();
var organism = { organismA: microbe,  organismB: bacteria };

but you cannot enforce their types:

// not an error (until you try to use the property as an Organism)
organism.organismA = "Not an Organism";   
organism.organismB = 123; 

organism.doesntEvenExist = microbe;
Sign up to request clarification or add additional context in comments.

Comments

1

If you are trying to create a constructor that will define properties on the instance that are also instances of the object, that is impossible. Lets say for example that this is your constructor:

function Organism () { this.organismA = new Organism(); }

Then trying to create a new instance of that will cause "RangeError: Maximum call stack size exceeded" and for good reason, because each time a new instance is created, it will try to create a new instance in itself, and so on until you will exceed the stack.

You can however do this in javascript:

function Organism () {}

var organism = new Organism();

organism.organismA = new Organism();
organism.organismB = new Organism();

// And now the instance and its properties are all Organism
console.log('organism is instance of Organism: ' + (organism instanceof Organism)); // true
console.log('organism.organismA is instance of Organism: ' + (organism.organismA instanceof Organism)); //true
console.log('organism.organismB is instance of Organism: ' + (organism.organismB instanceof Organism)); //true

// If you want you can wrap this in a factory

function createExtendedOrganism () {
  var organism = new Organism();

  organism.organismA = new Organism();
  organism.organismB = new Organism();
  
  return organism;

}

var extendedOrganism = createExtendedOrganism();

// Again the instance and its properties are all Organism
console.log('extendedOrganism is instance of Organism: ' + (extendedOrganism instanceof Organism)); //true
console.log('extendedOrganism.organismA is instance of Organism: ' + (extendedOrganism.organismA instanceof Organism)); //true
console.log('extendedOrganism.organismB is instance of Organism: ' + (extendedOrganism.organismB instanceof Organism)); //true

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.