6

I know there are a lot of OO javascript questions on SO and I a have been reading a lot of resources.... BUT it is still far my most long winded learning curve so far!

I am not classically trained sorry, hence I will have to just show you guys in c# an example of what I want to acheive.

I hope you can help!

public class Engine
{
    public int EngineSize;

    public Engine()
    {
    }
}
public class Car
{
    public Engine engine;

    public Car()
    {
        engine = new Engine();
    }
}

guys I am not really worried about the private/public & naming conventions of the above C# example.

All I want to know is how to replicate this structure in Javascript?

Thanks!

1
  • Is this supposed to be C#? What are the parens after public class ...? Commented Aug 24, 2009 at 15:33

4 Answers 4

16
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();
Sign up to request clarification or add additional context in comments.

1 Comment

I like this better than the other examples here because it uses javascript ability to provide a private namespace that isn't accessible from the outside. I'd have showed the same thing with the car as well, but at least your privateVar demonstrates information hiding.
3

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();

Comments

1

Here is an answer with ES6:

class Engine {
  // Constructor, size defaults to 1
  constructor(size = 1) {
    // Sets size to size
    this.size = size;
  }
  // Method
  start() {
    for (let i = 0; i < this.size, i++) {
      alert('Brrr');
    }
  }
}
class Car {
  // Constructor
  constructor(name) {
    // Sets engine to a new engine
    this.engine = new Engine();
    // Sets name to name
    this.name = name;
  }
  // Method
  introduceTo(name) {
    alert(`Hey ${name}, here is my car ${this.name}.`);
  }
}

let myCar = new Car('Ferrari');
myCar.engine = new Engine(5);
myCar.introduceTo('bro');
myCar.engine.start();

(I made some additions hopefully you like it)

Comments

0
function Engine(){ // this is constructor. Empty since Engine do nothing. 
}
Engine.prototype.EngineSize=null; // this is public property

function Car(){ // this is Car constructor. It initializes Engine instance and stores it in Engine public property
    this.Engine =new Engine();
}

Car.prototype.Engine =null;

When you will make new Car instance. Car constructor will create new instance of Engine and assign it to Engine property of Car instance.

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.