2

I searched on how to inherit an Object and I found that you can use the Create() function but it doesn't work.

function Animal(strength, team, id /*url*/ ) {
  this.strength = strength;
  this.team = team;
  this.id = id;
  this.can_move = function(position, destination) {
    this.destination = Board[destination[0]][destination[1]]
    if ((position[0] == destination[0] + 1) && (position[1] == destination[1]) ||
      (position[0] == destination[0] - 1) && (position[1] == destination[1]) ||
      (position[1] == destination[1] + 1) && (position[0] == destination[0]) ||
      (position[1] == destination[1] - 1) && (position[0] == destination[0])) {} else {
      console.log('false')
      return false
    }

    if (Board[destination[0]][destination[1]].animal != null) {
      if (this.can_eat(Board[destination[0]][destination[1]].animal)) {
        /*pass*/ } else {
        console.log('false')
        return false
      }
    }
    if (!this.obstacle()) {
      return false;
    }

    console.log('true')
    return true
  }
  this.can_eat = function(animal) {
    if (animal.strength <= this.strength) {
      return true
    } else {
      return false
    }
  }
  this.obstacle = function() {
    if (Board[destination[0]][destination[1]].type == "River1" || Board[destination[0]][destination[1]].type == "River2" ||
      Board[destination[0]][destination[1]].type == team + "_goal") {
      console.log("false");
      return false;
    }
  }
  this.move = function(position, destination) {
    //pass
  }
}

function Mouse(team, id) {

}
Mouse.prototype = Object.create(Animal.prototype);

Board is an array and if I do a new Mouse it doesn't know what can_move is:

Uncaught TypeError: Board[2][6].animal.can_move is not a function, animal is just an 
attribute of board, it contains Mouse.

Edit: Thanks,but now it gives me:

Cannot set property 'can_move' of undefined
at new Animal.
4
  • 5
    All the properties you've added are not on the Animal prototype, that's why Mouse does not inherit them through there. Commented Mar 29, 2021 at 15:27
  • 1
    you also need to call the Animal constructor from your Mouse constructor Commented Mar 29, 2021 at 15:29
  • 4
    Unless this is a part of your assignment, I'd recommend to look into classes, which are far easier to work with. Commented Mar 29, 2021 at 15:32
  • 1
    Small suggestion to paste your JS code here and fix what it says jshint.com Commented Mar 29, 2021 at 16:12

1 Answer 1

1

You need to make sure you're declaring your functions on the prototype of Animal

function Animal(){

}
Animal.prototype.can_move = function(){ return true; }

function Mouse(){

}
Mouse.prototype = Object.create(Animal.prototype);

const m = new Mouse();
console.log(m.can_move());

This is a lot easier using classes

class Animal {
   can_move(){ return true; }
}

class Mouse extends Animal {}

const m = new Mouse();
console.log(m.can_move());

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

1 Comment

I am gonna use classes instead, thank all of you for your support

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.