-4

I recently started learning JavaScript. but i don't have any Project Experience. Recently attended Interview on JavaScript:

He asked one question about Inheritance: should be look like this:

I need One Method Called Vehicle and another two Methods Like one is Two-Wheeler and another one four-wheeler Method.

If I create Instance for those two methods like Below

var two-wheeler1 = new two-wheeler("Shine","125 kph") ; 

another instance is like below

var four-wheeler1 = new four-wheller("audi","1000kph");

if I print the

 two-wheeler1.getWheeles()  //it should print 2 wheels

If i print the

four-wheeler1.getWheeles() //it Should print 4 Wheels

How to achieve this. Could you please any one help me?

Siva
  • 3
  • 4
  • 1
    Read all this document => https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript – Fefux Dec 21 '16 at 10:32
  • Possible duplicate of [Overriding methods in Javascript](http://stackoverflow.com/questions/15497259/overriding-methods-in-javascript) – Pritam Banerjee Dec 21 '16 at 10:33
  • Please note, that - is not valid as a name – Icepickle Dec 21 '16 at 10:33
  • Before you move ahead, and definitely before you go to any more interviews, go back and review your JS learning materials, and make sure you understand the difference between a method and a class, and also correct variable naming rules in JS. –  Dec 21 '16 at 11:15

2 Answers2

1

Presumably the interviewer and his company is programming in modern JS (ES6, or TypeScript). If not, you should probably find another company to interview. ES6 provides nice clean syntax for defining classes and inheritance relationships:

class Vehicle {
  constructor(brand, kmp) { 
    this.brand = brand;
    this.kmp = kmp;
  }

  getWheels() { return `${this.nWheels} wheels`; }
}

class TwoWheeler extends Vehicle {
  constructor(brand, kmp) {
    super(brand, kmp);
    this.nWheels = 2;
  }
}

class FourWheeler extends Vehicle {
  constructor(brand, kmp) {
    super(brand, kmp);
    this.nWheels = 4;
  }
}

By the way, in the rest of the world we call two-wheelers "motorcycles" or "scooters", and four-wheelers "cars".

  • Thanks for Help. For not only interviews for understand purpose also could you please help me out how to achieve the Using ES5 or Venila javascript. For me it is great help. I am requesting to you. Thanks! – Siva Dec 21 '16 at 11:34
0

Please review following code to under stand better OOP and Inheritance in JS:

function twoWheeler(brand, kmp) {
   this.brand=brand;
   this.kmp=kmp;
   this.getWheeles=function() {
     return "2 wheels"
   };
   this.getVehicleInfo=function() {
     return "Brand Name: " + this.brand + " Speed: " + this.kmp + " Type: 2   wheeler."
   };
  }

 function fourWheller(brand, kmp){
   this.brand=brand;
   this.kmp=kmp;
   this.getWheeles=function() {
     return "4 wheels"
   };
   this.getVehicleInfo=function() {
      return "Brand Name: " + this.brand + " Speed: " + this.kmp + " Type: 4 wheeler."
    };
  }

  fourWheller.fourWheller= new twoWheeler();
  fourWheller.prototype.constructor=fourWheller;

  var twoWheeler1 = new twoWheeler("Shine","125 kph");
  var fourWheller1 = new fourWheller("Audi","1000kph");

  console.log(twoWheeler1.getWheeles())
  console.log(fourWheller1.getWheeles())

  console.log(twoWheeler1.getVehicleInfo())
  console.log(fourWheller1.getVehicleInfo())
Hanif
  • 3,739
  • 1
  • 12
  • 18
  • I'm confused. You are deriving two-wheelers from four-wheelers? Where's the `Vehicle` class which was requested? What does `fourWheeler.fourWheeler = new twoWheeler();` do? Why do you need to set the constructor on the prototype for your own class--that happens automatically, right? By the way, it's "wheeler", not "wheller", and "wheels", not "wheeles". –  Dec 21 '16 at 11:19
  • I'm not giving the interviewer answer I just trying the teach him primary idea about OOP and Inheritance and it is present in my answer. – Hanif Dec 21 '16 at 11:19
  • @Hanif, Here we are rewriting the properties every function, So is it Performance wise is well to go? – Siva Dec 21 '16 at 11:19
  • Yes I think so. – Hanif Dec 21 '16 at 11:20
  • 1
    @Hanif *It is present in my answer* No, it's not. You should define a class `Vehicle`, and then derive `TwoWheeler` and `FourWheeler` from that. Any logic which is common should be in the superclass. Also, normally you would define methods on the prototype(s), not within the constructor. –  Dec 21 '16 at 11:21
  • @torazaburo, could you please help me to how to achieve Model for Problem? Thanks in advance – Siva Dec 21 '16 at 11:22