使用对象方法将javascript转换为另一个对象
我试着从我的两个类中创建两个对象,一个Person对象和一个Drink对象,然后我想调用我的drinking方法传递一个Drink对象,但我不知道如何,我该怎么做?这是我的代码,我不明白为什么它不工作使用对象方法将javascript转换为另一个对象,javascript,class,object,methods,Javascript,Class,Object,Methods,我试着从我的两个类中创建两个对象,一个Person对象和一个Drink对象,然后我想调用我的drinking方法传递一个Drink对象,但我不知道如何,我该怎么做?这是我的代码,我不明白为什么它不工作 function Drink(full, alcoholic){ this.alcoholic = alcoholic; this.full = full; } function Person(name, age) { this.name = name; this
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))
到底是什么不起作用?你期望的是什么?如果你想引用参数,只需要
Drink
,而不是this。Drink
相比之下,你可能还想使用this.age
而不是age
。谢谢你,迈克,成功了,=)。也谢谢你的回答