Writing JavaScript according to SOLID

前端 未结 5 1477
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-29 20:25

Have any one used the SOLID programming principle (or any of it\'s parts) while developing JavaScript?

I\'ve just started up on reading on it but can\'t seem to find any

5条回答
  •  伪装坚强ぢ
    2021-01-29 20:52

    Most of SOLID principals depend on abstraction, In other languages such as Java, we can do abstractions by using interfaces, And Javascript doesn't have interfaces, Does that means that we can't achieve SOLID principles in Javascript? The answer is No. This is a great article to clarify the things.

    http://www.yusufaytas.com/achieving-abstraction-in-javascript/

    In consequence, JavaScript is a weakly typed language and does not have classical support for abstraction. We have tried to achieve abstraction in JavaScript by defining interfaces and using them. Nevertheless, interfaces do not affect our code at all. They are nothing more than a way of documenting code for human beings. All the examples we have shown above would work exactly the same even if we completely remove the interfaces. However, it is about making roles explicit. This increases readability, understandability, and maintainability of the code.

    In general, The main goal for SOLID principles is to make the code readable and easy to modify, Applying SOLID to your code is all about trying not to repeat yourself and make your code nicer and cleaner.

提交回复
热议问题