0

I have a "class" defined:

function Car(door, engine) {
    this.door = door;
    this.engine = engine;

    function setDoor(door) {
        this.door = door;
    }
    function getDoor(door) {
        return door;
    }
    function setEngine(engine) {
        return engine;
    }
    function getEngine() {
        return engine;
    }
}

I want to extract data from an object in javascript, in the same file I have another "class" and the function to add.

function bodyCar(car) {
    newCar = new Array();
    function addCar(car) {
        newCar = car.door;
        newCar = car.engine;
        return "The new car have " + car.door + " doors. engine " + car.engine;
    }
}

the call to the class and function:

var myCar = new bodyCar();
myCar.addCar(new Car(5,"2200cc"));
document.write(myCar);

But it does not work for me. It gives a type error "myCar.addCar is not a function".

thanks for your help.

6
  • Are you sure the context of the private methods in your constructor will be the constructor's context? Commented Dec 23, 2015 at 14:38
  • addCar is not a property of bodyCar... Commented Dec 23, 2015 at 14:39
  • You declare newCar = new Array(), but then you assign car.door and car.engine to the array in addCar? Commented Dec 23, 2015 at 14:40
  • addCar() is a local function in bodyCar Commented Dec 23, 2015 at 14:40
  • On top of everything else, also note that all instances of bodyCar share the same newCar. Commented Dec 23, 2015 at 14:41

2 Answers 2

2

That's not how you define methods in class-style Javascript. The functions you define inside the constructor function only exist during the call to the constructor.

If you want to add "instance methods" to the "class", you have to put them on the constructor function's prototype object:

function Car(door, engine) {
  this.door = door;
  this.engine = engine;
}

Car.prototype.setDoor = function(door) {
  this.door = door;
}

Car.prototype.getDoor = function() {
  return this.door;
}

Car.prototype.setEngine = function(engine) {
  this.engine = engine;
}

Car.prototype.getEngine = function() {
  return this.engine;
}

That said, your logic in the second class (creating and then throwing away an Array) doesn't make sense to me; I'm not sure what your actual goal is. But hopefully the above helps get you closer.

Sign up to request clarification or add additional context in comments.

Comments

2

If you're trying to create OOP-style class methods, then you have some options:

create a function property of the class

function bodyCar(car) {
  newCar = new Array();
  this.addCar = function(car) {
    // do addCar method stuff here
  }
}

or if you want to use the prototype method approach (better memory management)

function bodyCar(car) {
  newCar = new Array();
}

bodyCar.prototype.addCar = function(car) {
    // do addCar method stuff here
}

Likewise, the same approach should be used for the methods on your base car class, like setDoor and getDoor etc.

Further, if you want OOP-style "inheritance", you should opt for the prototype approach, and use Object.create()

Good luck!

1 Comment

I am just realizing that your logic has does not make any practical sense here, as you're creating the newCar array, then setting that variable to the door, then setting it again to the engine.... Either way, the approach mentioned here is what you're going for

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.