0

Hey guys I am new to JavaScript and was learning prototypes in JavaScript and I faced the error:

main.js:19 Uncaught TypeError: bmw.move is not a function

with the following code:

function Car() {
  this.model = 'BMW';
  this.speed = 1000;

  Car.prototype.move = function () {
    alert('Move the car');
  }
}
function BMW() {

}

BMW.prototype = Object.create(Car.prototype);
let bmw = new BMW();
bmw.move();
mirzhal
  • 159
  • 1
  • 2
  • 14
  • There are a couple of errors in the above. You should never assign a function to `Car.prototype` from *within* a call to `Car`. The inheritance relationship between `BMW` and `Car` is only half-implemented. See the linked question's answers for how to properly set up an inhertance relationship using constructor functions, either in ES5 or (since it's 2019) using `class` syntax. – T.J. Crowder May 27 '19 at 09:08
  • function Car() { this.model = 'BMW'; this.speed = 1000; } Car.prototype.move = function () { alert('Move the car'); } function BMW() { } BMW.prototype = Object.create(Car.prototype); let bmw = new BMW(); bmw.move(); – Anil Kumar Moharana May 27 '19 at 09:09
  • @Anilm - No, that's still missing at least two pieces, please see the linked question's answers. – T.J. Crowder May 27 '19 at 09:15

0 Answers0