gpt4 book ai didi

javascript - 它工作不正常,输出未定义?

转载 作者:行者123 更新时间:2023-12-03 07:26:36 24 4
gpt4 key购买 nike

function Rabbit(adjective) {
this.adjective = adjective;
this.describeMyself = function() {
console.log("I am a " + this.adjective + " rabbit");
};
}

// now we can easily make all of our rabbits
rabbit1 = new Rabbit("fluffy");
rabbit2 = new Rabbit("happy");
rabbit3 = new Rabbit("sleepy");

console.log(rabbit1.describeMyself());
console.log(rabbit2.describeMyself());
console.log(rabbit3.describeMyself());

输出:

I am a fluffy rabbit
undefined
I am a happy rabbit
undefined
I am a sleepy rabbit
undefined

如果我将其作为 .js 文件而不是控制台执行,它会停止未定义吗?或者会是一样的吗?

最佳答案

您正在尝试 console.log 一个不会返回任何内容的函数,因此结果显示为未定义。您只需调用该函数,因为其中已经有 console.log .. 试试这个

rabbit1.describeMyself();rabbit2.describeMyself();rabbit3.describeMyself();

关于javascript - 它工作不正常,输出未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35979041/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com