Are Interfaces in JavaScript necessary?

前端 未结 5 1079
再見小時候
再見小時候 2021-02-04 00:31

I suppose this could apply to any dynamic language, but the one I\'m using is JavaScript. We have a situation where we\'re writing a couple of controls in JavaScript that need t

5条回答
  •  爱一瞬间的悲伤
    2021-02-04 01:08

    This is the same for PHP; you don't really need interfaces. But they exist for architectural needs. In PHP, you can specify type hints for functions which can be useful.

    Second, an interface is a contract. It's a formal contract that all objects from this interface have those functions. Better to ensure that your classes meet those requirements than to remember: "mm, this class has isEnabled() but the other one is checkIfEnabled()". Interfaces help you to standardise. Others working on the derived object don't have to check whether the name is isEnabled or checkIfEnabled (better to let the interpreter catch those problems).

提交回复
热议问题