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

};
djfkdjfkd39939
  • 293
  • 1
  • 2
  • 7
  • this might be helpful http://stackoverflow.com/questions/502366/structs-in-javascript – emrahozkan Apr 14 '15 at 04: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. – Mike 'Pomax' Kamermans Apr 14 '15 at 04:01
  • Simply assign the correct values to the properties. – Felix Kling Apr 14 '15 at 04:03

2 Answers2

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;
Thilo
  • 257,207
  • 101
  • 511
  • 656
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
Aviv Shaked
  • 762
  • 3
  • 8