pein
pein

Reputation: 13

javascript using a object method into another object

im trying to make 2 objects from my 2 classes, a Person object and a Drink object, then i want to call my drinking method passing a Drink objectbut i dont know how, how can i do that? here is my code, i cant see why it is not working

function Drink(full, alcoholic){
    this.alcoholic = alcoholic;
    this.full = full;
}

function Person(name, age) {
    this.name = name;
    this.age = age;
    this.drinking = function drinking(Drink) {
        if (age >= 18) {
            this.Drink.full = false;
        } else {
            console.log("you cannot drink because of your age")
            this.Drink.full=true;
        };
    }
}

John = new Person("John",24);
Sam = new Person("Sam",2);
x = new Drink(true,true);

console.log(x)
console.log(John.drinking(x))
console.log(Sam.drinking(x))

Upvotes: 1

Views: 52

Answers (1)

fdfey
fdfey

Reputation: 587

Remove this on this.Drink

function Drink(full,alcoholic){
  this.alcoholic  = alcoholic;
  this.full   = full;
}

function Person(name,age){
  this.name = name;
  this.age = age;
  this.drinking= function drinking(drink) {
    if (age>=18) {
        drink.full=false;    
    } else {
      console.log("no puede tomar")
      drink.full=true;
    };
  }
}

John = new Person("John",24);
Sam = new Person("Sam",2);
x = new Drink(true,true);

console.log(x)
console.log(John.drinking(x))
console.log(Sam.drinking(x))

Upvotes: 1

Related Questions