Javascript 为什么类实例方法在调用后导致未定义。超时功能正确吗?

Javascript 为什么类实例方法在调用后导致未定义。超时功能正确吗?,javascript,javascript-objects,Javascript,Javascript Objects,aeri.attack/aeri.magicatack生成attack()函数。但是如果我试图调用函数,它会导致未定义。为什么 我试着做一个角色实例,看看它是否只是子类,但不是 PS如果有人能告诉我,如果我做的设置超时功能的权利,这将是可怕的,我会永远爱你 PS PS我无法测试超时方法是否有效,因为我的任何方法都不起作用 class Character { constructor(name, weapon) { this.name = name; this

aeri.attack/aeri.magicatack生成attack()函数。但是如果我试图调用函数,它会导致未定义。为什么

我试着做一个角色实例,看看它是否只是子类,但不是

PS如果有人能告诉我,如果我做的设置超时功能的权利,这将是可怕的,我会永远爱你

PS PS我无法测试超时方法是否有效,因为我的任何方法都不起作用

class Character {
    constructor(name, weapon) {
        this.name = name;
        this.weapon = weapon;
    }

    attack(){
        this.name + ' attacks with ' + this.weapon;
    }
}

class Elf extends Character {
    constructor(name, weapon, type) {
        super(name, weapon);
        this.type = type;
    }

    magicAttack() {
        this.name + ' chants ';
        setTimeout(function(){this.type; }, 3000);


    }
}

const aeri = new Elf("Aeri", "bow", "air")
const bob = new Character("Bob", "yawn")

只需在代码中添加console.log:

类字符{
建造师(姓名、武器){
this.name=名称;
这个武器=武器;
}
攻击(){
console.log(this.name+'使用'+this.wealth'攻击);
}
}
类Elf扩展字符{
构造函数(名称、武器、类型){
超级(姓名、武器);
this.type=type;
}
magicatack(){
log(this.name+'chants');
设置超时(()=>{
console.log(this.type);
}, 3000);
}
}
const aeri=新精灵(“aeri”、“bow”、“air”)
const bob=新角色(“bob”,“哈欠”)
bob.attack()

aeri.magicatack()
有几个原因可以解释为什么原始帖子中的代码没有达到OP所期望的效果

class Character {
    constructor(name, weapon) {
        this.name = name;
        this.weapon = weapon;
    }

    attack(){
        this.name + ' attacks with ' + this.weapon;  //  (1)
    }
}

class Elf extends Character {
    constructor(name, weapon, type) {
        super(name, weapon);
        this.type = type;
    }

    magicAttack() {
        this.name + ' chants ';                      //  (2)
        setTimeout(function(){this.type; }, 3000);   //  (3)


    }
}

const aeri = new Elf("Aeri", "bow", "air")
const bob = new Character("Bob", "yawn")
(1)
attack()
方法缺少
return
语句

(2)
this.name+'chants'语句没有执行任何操作,因为串联的结果没有分配给变量或传递给方法,所以它被简单地丢弃

(3) 传递给
setTimeout
的匿名函数引用了
this.type
,但是给定的
type
在此上下文中未定义(因为
this
实际上是匿名函数本身)

要查看调用该方法的结果,需要对原始代码示例应用以下更改:

class Character {
    constructor(name, weapon) {
        this.name = name;
        this.weapon = weapon;
    }

    attack(){
        return this.name + ' attacks with ' + this.weapon;    //  (1)
    }
}

class Elf extends Character {
    constructor(name, weapon, type) {
        super(name, weapon);
        this.type = type;
    }

    magicAttack() {
        const chant = this.name + ' chants ';                  //  (2)
        setTimeout(function(){ console.log(chant); }, 3000);   //  (3)

    }
}

const aeri = new Elf("Aeri", "bow", "air")
const bob = new Character("Bob", "yawn")

aeri.magickAttack();
希望这有帮助


Jan

您希望在哪里看到方法的输出?是否要打印文本?或者说重点是什么?如果你想打印它,你必须使用
console.log(“你的文本”)@sean是的,如果我在console@Esdras天哪,我真傻。是的,我需要退货。唉。谢谢你指出我的错误。我讨厌我自己没问题@Mugs,我知道你的感受,哈哈。很高兴为你提供帮助谢谢你提供了非常详细和彻底的答案。真的很感激!有没有办法将this.name+“chants”和timeout函数分开,但仍然返回这两个函数?澄清一下,我的目标是让它输出圣歌,几秒钟后再输出咒语。当然,
console.log()
函数会接收要打印到控制台的内容,因此您可以说:
console.log(this.name+'chants')
setTimeout(函数(){console.log('some-spell');},3000)你是最棒的。谢谢你,伙计。我有一部分为自己没有想到这一点而感到羞愧,但我想我只是需要更多的实践来解决实际问题,以巩固我所学到的东西。没有理由感到羞愧,我们都经历过:-)祝编码愉快!另外,请查看Mozilla指南-