首页 > 试题广场 >

如下代码,catInstance.fn() 和 Cat.fn

[单选题]
如下代码,catInstance.fn() 和 Cat.fn() 分别输出什么?
class Dog {
    static fn() {
        console.log('super zhihu')
    }
}

class Cat extends Dog {
   
}

const catInstance = new Cat();

catInstance.fn();
Cat.fn();


  • catInstance.fn() => TypeError: catInstance.fn is not a function , Cat.fn() => 'super zhihu'
  • catInstance.fn() => 'super zhihu' , Cat.fn() => 'super zhihu'
  • catInstance.fn() => undefined, Cat.fn() => TypeError: Cat.fn is not a function
  • catInstance.fn() => TypeError: catInstance.fn is not a function, Cat.fn() => TypeError: Cat.fn is not a function
js的静态函数只能让类名调用,对象调用会出错,这个和java不一样
编辑于 2024-04-12 21:18:57 回复(0)