What are the pros and cons of using classes vs interfaces in angular ?what is the best practice and when to use classes and when to use interfaces in angular?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
I guess, this part from the Angular Style Guide says it all:
Consider using a
class
instead of aninterface
for services and declarables (components, directives, and pipes).Consider using an
interface
for data models.
回答2:
I'll try to give you the short answer, that should give you the overall picture by demonstrating the key differences:
Interfaces are not compiled to javascript.
vs
Classes are compiled to javascript classes.
Interfaces are used for tooling (Intellisense/autocomplete) only.
vs
Classes are part of runtime program logic.