按照SOLID编写JavaScript
有人在开发 JavaScript 时使用过 SOLID 编程原则(或其任何部分)吗?
我刚刚开始阅读它,但似乎找不到任何人将它用于 JS。我发现唯一易于实施/使用的部分是“单一责任原则”。
我正在寻找的是使用这些原则的文章或示例。是否有任何争论为什么人们不应该使用某些部件?
例如,“接口隔离原则”指出“许多客户端特定接口比一个通用接口更好的概念”。
但据我所知,JS 中不存在接口之类的东西(如果有的话就好了)。
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 anyone that used it for JS. The only part I find easy to implement/use is the "Single responsibility principle".
What I'm looking for is articles or example where these principles are used. And are there any argument's why one shouldn't use some parts?
For example the "Interface segregation principle" says that "the notion that many client specific interfaces are better than one general purpose interface."
But from my knowledge there's no such thing as interfaces in JS (nice if it would be).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看起来 Derek Greer 正试图在 Fresh Brewed Code 上发表的有关 SOLID JavaScript 的文章系列中尝试解决这个问题:
It looks like Derek Greer is attempting to take a stab at this with his article series on SOLID JavaScript at Fresh Brewed Code:
JavaScript 有时会因为不如 C++、C# 和 Java 等语言而受到不好的评价,而事实上它是一种非常强大的函数式编程语言,还具有面向对象的功能(尽管它并没有真正被归类为面向对象)
。开发人员看不起它,因为他们中的许多人习惯于看到糟糕的编程实践以及由此导致的 JavaScript 中的错误行为。由于某种未知的原因,在客户端方面的马虎似乎更容易被接受。这是我想要改变的事情。
我相信这些 SOLID 原则是可靠的。 (没有双关语的意思)。如果遵循这些约定,您就不太可能积累因草率代码、快捷方式和无架构而造成的技术债务。您的代码将更易于维护、更可重用、更模块化、耦合性更小、并且可伸缩和可扩展。当你的产品被设计出来而不是鲁莽地拼凑在一起时,你还将为展示 JavaScript 的全部力量做出贡献。
本文档描述了 SOLID 的基础知识。无论您指的是 C++、Java、JavaScript 还是任何其他面向对象的语言,都适用相同的规则。
代码项目 - SOLID 面向对象编程原则
以下是有关colourcoding.net 上的 JavaScript 概念。
JavaScript sometimes gets a bad rap as being sub-par to those such as C++, C#, and Java, when in fact it's a very powerful functional programming language that also has object oriented capabilities (although it's not really classified as object oriented)
Perhaps many developers look down on it because so many of them are used to seeing poor programming practices and consequently, buggy behavior in JavaScript. For some unknown reason, it seems more acceptable to be sloppy on the client side. This is something I would like to change.
I believe these SOLID principles are solid. (No pun intended). If you follow these conventions, you will be less likely to accumulate technical debt created by sloppy code, shortcuts, and no architecture. Your code will be more maintainable, more reusable, more modular, less tightly coupled, and scalable and extensible. You'll also be contributing to demonstrating the full power of JavaScript when your product is engineered instead of just recklessly slapped together.
This document describes the fundamentals of SOLID. The same rules apply whether you're referring to C++, Java, JavaScript, or any other object-oriented language.
Code Project - The SOLID Object Oriented Programming Principles
Here is some more information on JavaScript concepts on colourcoding.net.
这个接受的答案是有缺陷的。我建议阅读下面 Ryan Rensford 链接的五篇文章。 上一篇文章得出以下结论我未能传达的内容(由我强调):
SOLID 用于面向对象编程。 JavaScript 是一种基于原型的语言,但允许以 OOP 方式进行编程(如果您确实努力这样做)。许多人认为,您不应该尝试将您所学的语言范式(例如 C++/C#/Java)强加给其他语言(JavaScript)。这是一篇关于 JS 中的 OOP 的文章,也得出了这个结论。< /del>Prototype.js 中有一些面向 OOP 的方法,CoffeeScript ,以及 John Resigs 简单 JavaScript 继承(每个都有自己的陷阱)。但是 SOLID 的术语(接口、抽象)很难以正确的方式应用于普通 JavaScript。您将能够应用“S”,也许还可以应用“L”(这是很好的概念)。但更进一步,需要像接口这样的构造(无论如何,在动态语言中很难找到,契约可能会起作用)以及限制继承/修改的能力。This accepted answer is flawed. I recommend to read the five articles linked to by Ryan Rensford below. The last article comes to the following conclusion which I failed to communicate (emphasis by me):
SOLID is meant for object-oriented programming. JavaScript is a prototype-based language but allows programming in an OOP-manner (if you really try hard to do so). Many people believe that you shouldn't try to force paradigms of languages you learned (like C++/C#/Java) onto others (JavaScript). Here's an article on OOP in JS which also comes to that conclusion.There are some approaches to OOP in Prototype.js, CoffeeScript, and John Resigs Simple JavaScript Inheritance (each with its own traps).But the terminology (interfaces, abstraction) of SOLID is difficult to apply to vanilla JavaScript in a proper manner. You will be able to apply "S", and maybe the "L" (which are good concepts). But going further would require constructs like interfaces (which are hard to find in dynamic languages anyway, contracts might work) and the ability to restrict inheritance/modification.本演示文稿:Derick Bailey 的SOLID JavaScript In A Wobbly(万维网)演示了如何使用 SOLID 编程原则在开发 javascript 时。
他多次明确地解决了 javascript 中的接口问题。在 javascript 中,接口更多的是一种约定,因此如何定义它们取决于您。您可以编写一个空对象定义(将其视为抽象类或接口)。或者您可以依赖您的协议/合同的文档。
在更概念化的层面上,对象的方法签名隐式定义了它的接口,因此在 javascript 中,您通常可以通过“鸭子类型”来完成,并且仍然坚持 SOLID。 (!) 以下是演示文稿中“接口隔离原则”的示例,演示了这一点:
此代码以及演讲中的所有其余示例代码都发布在 GitHub 上: com/derickbailey/solid-javascript" rel="nofollow">https://github.com/derickbailey/solid-javascript
This presentation: SOLID JavaScript In A Wobbly (World Wide Web) by Derick Bailey demonstrates how to use SOLID programming principles while developing javascript.
He explicitly addresses the issue of interfaces in javascript several times. In javascript, interfaces are more of a convention, so it's up to you how to define them. You can write an empty object definition (think of it like an abstract class or an interface). Or you can rely on documentation for your protocols/contracts.
At a more conceptual level, an object's method signatures implicitly define it's interface, so in javascript you can often get by with "duck-typing" and still adhere to SOLID. (!) Here's an example of the "interface segregation principle" from the presentation that demonstrates this:
This code and all of the rest of the example code from the talk is posted on GitHub: https://github.com/derickbailey/solid-javascript
大多数SOLID原则依赖于抽象,在其他语言如Java中,我们可以使用接口进行抽象,而Javascript没有接口,这是否意味着我们无法在Javascript中实现SOLID原则?
答案是否定的。
这是一篇澄清事情的好文章。
一般来说,SOLID 原则的主要目标是使代码可读且易于修改,将 SOLID 应用于您的代码就是尽量不重复自己并使您的代码更好、更干净。
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.
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.