Predict instanceof 输出
预测输出 中等
题目
什么做 这个 code print?
代码
class Cat { meow() { return "meow!"; } }
class Dog { bark() { return "woof!"; } }
function speak(animal: Cat | Dog) {
if (animal instanceof Cat) return animal.meow();
return animal.bark();
}
console.log(speak(new Cat()));
console.log(speak(new Dog()));
提示
instanceof 检查类 constructor at 运行时.
解释
speak(新 Cat()) — animal instanceof Cat 是真, 返回 "meow!". speak(新 Dog()) — animal instanceof Cat 是假, falls 通过到 animal.bark(), 返回 "woof!". 输出: meow! and woof! on separate 行.
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。