1

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))
2
  • 1
    What exactly is not working? What are you expecting? Commented Apr 27, 2016 at 18:38
  • It's just Drink if you want to refer to the parameter, not this.Drink Commented Apr 27, 2016 at 18:57

1 Answer 1

1

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))
Sign up to request clarification or add additional context in comments.

2 Comments

In contrast, you also might want to use this.age instead of age.
thank you Mike, that made it, =). Also thx Bergi for answering

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.