这是一个代码,它的作品很好,这就是它的来历。但是,当我在浏览器中尝试相同的代码时,它总是返回未定义的。
<script>
function Cat(name, breed) {
this.name = name;
this.breed = breed;
}
Cat.prototype.meow = function() {
console.log('Meow!');
};
var cheshire = new Cat("Cheshire Cat", "British Shorthair");
var gary = new Cat("Gary", "Domestic Shorthair");
alert(console.log(cheshire.meow));
alert(console.log(gary.meow));
</script>发布于 2016-06-28 14:14:04
您将console.log()的结果传递给alert,但它不会返回任何内容,所以您将undefined传递给alert。
要么只使用alert,要么只使用console日志,不要将其中一个传递给另一个。
您的meow函数已经登录到控制台,因此再次这样做是没有意义的。很可能你想要的是:
cheshire.meow();
gary.meow();请注意,由于meow是一个函数,所以您可能需要实际调用它,而不仅仅是打印函数本身。
https://stackoverflow.com/questions/38078610
复制相似问题