Object Oriented Javascript - How To Define A Class Within A Class? from a C# example

function Engine(size) {
    var privateVar;

    function privateMethod () {
      //...
    }

    this.publicMethod = function () {
       // with access to private variables and methods
    };

    this.engineSize = size; // public 'field'
}

function Car() { // generic car
    this.engine = new Engine();
}

function BMW1800 () {
  this.engine =  new Engine(1800);
}

BMW1800.prototype = new Car(); // inherit from Car


var myCar = new BMW1800();

So you really just want to know how one object can contain another? Here's a very simple conversion of your sample:

function Engine()
{
    this.EngineSize=1600;
}

function Car()
{
    this.engine=new Engine();
}

var myCar=new Car();

Tags:

Javascript

Oop