我试图从我的2个类中创建2个对象,一个Person对象和Drink对象,然后我想调用传递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) {
            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))

最佳答案

在此将其删除。

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))

09-16 15:01