根据 SOLID 编写 JavaScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4711851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:36:53  来源:igfitidea点击:

Writing JavaScript according to SOLID

javascriptsolid-principles

提问by fredrik

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

有没有人在开发 JavaScript 时使用过 SOLID 编程原理(或其任何部分)?

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".

我刚刚开始阅读它,但似乎找不到任何人将它用于 JS。我发现唯一易于实施/使用的部分是“单一责任原则”。

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).

但据我所知,在 JS 中没有接口这样的东西(如果有就好了)。

回答by Ryan Ransford

It looks like Derek Greer is attempting to take a stab at this with his article series on SOLID JavaScript at Fresh Brewed Code:

看起来 Derek Greer 正试图通过他在 Fresh Brewed Code 上的关于 SOLID JavaScript 的系列文章尝试解决这个问题:

  1. The Single Responsibility Principle
  2. The Open/Closed Principle
  3. The Liskov Substitution Principle
  4. The Interface Segregation Principle
  5. The Dependency Inversion Principle
  1. 单一职责原则
  2. 开放/封闭原则
  3. 里氏替换原则
  4. 接口隔离原则
  5. 依赖倒置原则

回答by jmort253

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)

JavaScript 有时会因为低于 C++、C# 和 Java 等语言而受到批评,但实际上它是一种非常强大的函数式编程语言,还具有面向对象的功能(尽管它并没有真正归类为面向对象)

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.

也许许多开发人员看不起它,因为他们中的许多人已经习惯于看到糟糕的编程实践,从而看到 JavaScript 中的错误行为。由于某种未知的原因,在客户端马虎似乎更容易接受。这是我想改变的。

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.

我相信这些 SOLID 原则是可靠的。(没有双关语意)。如果您遵循这些约定,您将不太可能积累由草率代码、快捷方式和没有架构造成的技术债务。您的代码将更易于维护、更可重用、更模块化、更紧密耦合、可伸缩和可扩展。当您的产品经过精心设计而不是鲁莽地拼凑在一起时,您还将为展示 JavaScript 的全部功能做出贡献。

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.

本文档描述了 SOLID 的基础知识。无论您是指 C++、Java、JavaScript 还是任何其他面向对象的语言,都适用相同的规则。

Code Project - The SOLID Object Oriented Programming Principles

代码项目 - SOLID 面向对象编程原则

Here is some more information on JavaScript concepts on colourcoding.net.

以下是colourcoding.net上有关JavaScript 概念的更多信息。

回答by Marcel Hymanwerth

This accepted answer is flawed. I recommend to read the five articles linked to by Ryan Rensford below. The last articlecomes to the following conclusion which I failed to communicate (emphasis by me):

这个公认的答案是有缺陷的。我建议阅读下面由 Ryan Rensford 链接的五篇文章。在上一篇文章得出以下结论,我无法通讯(由我强调):

While in the course of our examination we saw variations in how the SOLID design principles apply to JavaScript over other languages, each of the principles were shown to have some degree of applicabilitywithin JavaScript development.

虽然在我们的检查过程中,我们看到了 SOLID 设计原则如何应用于 JavaScript 而不是其他语言的变化,但每个原则都被证明在 JavaScript 开发中具有一定程度的适用性

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 JSwhich also comes to that conclusion.

SOLID 适用于面向对象的编程。JavaScript 是一种基于原型的语言,但允许以 OOP 方式进行编程(如果您真的很努力的话)。许多人认为你不应该试图将你所学的语言范式(如 C++/C#/Java)强加给其他语言(JavaScript)。这是一篇关于JS中的OOP的文章,也得出了这个结论。

There are some approaches to OOP in Prototype.js, CoffeeScript, and John Resigs Simple JavaScript Inheritance(each with its own traps).

在 Prototype.js、CoffeeScriptJohn Resigs Simple JavaScript Inheritance 中有一些 OOP 方法(每个都有自己的陷阱)。

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.

但是 SOLID 的术语(接口、抽象)很难以适当的方式应用于 vanilla JavaScript。您将能够应用“S”,也许还有“L”(这是很好的概念)。但是更进一步需要接口之类的结构(无论如何在动态语言中很难找到,契约可能有效)和限制继承/修改的能力。

回答by wxactly

This presentation: SOLID JavaScript In A Wobbly (World Wide Web)by Derick Bailey demonstrates how to use SOLID programming principles while developing javascript.

本演示文稿:Derick Bailey 的SOLID JavaScript In A Wobbly(万维网)演示了如何在开发 javascript 时使用 SOLID 编程原则。

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 documentationfor your protocols/contracts.

他多次明确地解决了 javascript 中的接口问题。在 javascript 中,接口更像是一种约定,因此如何定义它们取决于您。你可以编写一个空的对象定义(把它想象成一个抽象类或一个接口)。或者,您可以依赖协议/合同的文档

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:

在更概念化的层面上,对象的方法签名隐式定义了它的接口,因此在 javascript 中,您通常可以通过“duck-typing”获得并仍然坚持 SOLID。(!) 这是演示文稿中“接口隔离原则”的一个示例,演示了这一点:

// Copyright 2014 Muted Solutions, LLC, and freely distributable and re-usable under the MIT License

// JavaScript has no interface construct ...
// sooooooo..... use inheritance instead? o_O

// Rectangle
// ---------

function Rectangle(){}

Rectangle.prototype.area = function(){
  return this.height * this.width;
};

Rectangle.prototype.setHeight = function(height){
  this.height = height;
};

Rectangle.prototype.setWidth = function(width){
  this.width = width;
};

// Square
// ------

function Square(){}

Square.prototype.area = function(){
  return this.size * this.size;
};

Square.prototype.setSize = function(size){
  this.height = size;
  this.width = size;
};

This code and all of the rest of the example code from the talk is posted on GitHub: https://github.com/derickbailey/solid-javascript

此代码和演讲中的所有其余示例代码发布在 GitHub 上:https: //github.com/derickbailey/solid-javascript

回答by Maysara Alhindi

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.

SOLID原理大部分依赖于抽象,在Java等其他语言中,我们可以通过接口来进行抽象,而Javascript没有接口,是不是就不能在Javascript中实现SOLID原理呢?答案是否定的。这是一篇很好的文章来澄清这些事情。

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.

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

因此,JavaScript 是一种弱类型语言,没有对抽象的经典支持。我们试图通过定义接口并使用它们来在 JavaScript 中实现抽象。尽管如此,接口根本不会影响我们的代码。它们只不过是为人类记录代码的一种方式。即使我们完全删除了接口,我们上面展示的所有示例也将完全相同。然而,它是关于使角色明确。这增加了代码的可读性、可理解性和可维护性。

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.

一般来说,SOLID 原则的主要目标是使代码可读且易于修改,将 SOLID 应用于您的代码就是尽量不重复自己并使您的代码更好、更干净。