0

I'm new in programming and I'm learning JavaScript OOP, trying to make a game with tanks. I have some code but it doesn't work properly and I need some help to understand how it works. Please check it and tell me how to solve the problem because I want to add a few more kinds of tanks but before that I need to fix the code.

var Tank = (function () {
    function Tank(name) {
        this._name = name;
    }

    Tank.prototype.getWeight = function () { return this._weight; }
    Tank.prototype.getName = function () { return this._name; }

    return Tank;
}());



var SmallTank = (function () {
    this.prototype = Object.create(Tank.prototype);

    function SmallTank(name) {
        Tank.apply(this._name);
    }

    SmallTank.prototype._weight = 2;

    return SmallTank;
}());

var myTank = new SmallTank("Aleks Tank");

console.log(myTank.getWeight());
Baklap4
  • 3,914
  • 2
  • 29
  • 56
  • 3
    "tell me how to resolve the problem" - what's the problem? – Ja͢ck Dec 09 '14 at 08:34
  • 2
    `this` in selfexecuted function reference to global object - in this case - _window_, so `this.prototype` is equals `window.prototype` – Grundy Dec 09 '14 at 08:37
  • the problem is the this.prototype = Object create; line. Think, what is 'this' refering to? What is the current SCOPE? the this refers to the IIFE. – Pinoniq Dec 09 '14 at 08:38
  • I expect to see the wight in the console, instead of that I have only: undefined is not a function – user3531532 Dec 09 '14 at 08:38
  • what I have to write instead of this ? or maybe I have to delete that line ? – user3531532 Dec 09 '14 at 08:43
  • SmallTank.prototype = Object.create(Tank.prototype); something like this ? – user3531532 Dec 09 '14 at 08:49
  • The problem is resolved. Thank you to all of you ! – user3531532 Dec 09 '14 at 08:51
  • Maybe the following answer can help you understand about inheritance, prototype, constructor functions and mix ins: http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711 – HMR Dec 09 '14 at 11:42

2 Answers2

2

It seems that you're just trying to do some kind of inheritance; typically you do this by assigning a parent instance to the prototype of the child.

I think you will want something like this:

var SmallTank = (function () {
  function SmallTank(name) {
      Tank.call(this, name);
      this._weight = 2;
  }
  SmallTank.prototype = new Tank();

  return SmallTank;
}());

Alternatively you can assign Object.create(Tank.prototype).

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • Big Thanks to you, I just have the name in the console too :) thank you again for you help – user3531532 Dec 09 '14 at 08:56
  • I would still prefer object.create since it leaves Tank instance specific members out off SmallTank prototype – HMR Dec 09 '14 at 11:41
1

Here is another way of doing what it looks like you are attempting to do, following the Mozilla guide: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript

var Tank = function (name) {
    this.name = name;
};
Tank.prototype.getName = function () { return this.name; };


var SmallTank = function (name) {
    Tank.call(this, name);
    this.weight = 2;
};
SmallTank.prototype = Object.create(Tank.prototype);
SmallTank.prototype.constructor = SmallTank;
SmallTank.prototype.getWeight = function () { return this.weight; };

var myTank = new SmallTank("Aleks Tank");
console.log(myTank.getName());
console.log(myTank.getWeight());
JackDev
  • 4,891
  • 1
  • 39
  • 48