重构javascript

In smaller React projects, keeping all of your component methods in the components themselves works well. In medium-sized projects, you may find yourself wishing you could get those methods out of your components and into a “helper”. Here, I’ll show you how to use a Class (instead of exporting individual functions and variables) to organize your code.

在较小的React项目中,将所有组件方法保留在组件本身中会很好地工作。 在中型项目中,您可能会希望自己可以将这些方法从组件中提取出来,并放入“帮助器”中。 在这里,我将向您展示如何使用类(而不是导出单个函数和变量)来组织代码。

Note: I work in React so that’s the example we’ll discuss here.

注意 :我在React中工作,这就是我们将在这里讨论的示例。

典型重构 (Typical refactor)

In a typical refactor, you’d take a function on the component and move it to another helper.

在典型的重构中,您将在组件上使用一个函数并将其移至另一个帮助器。

From:

从:

const MyComponent = () => {const someFunction = () => 'Hey, I am text'return (<div>{someFunction()}</div>)
}

To:

至:

import { someFunction } from 'functionHelper.js'
const MyComponent = () => {return (<div>{someFunction()}</div>)
}

and

export const someFunction = () => 'Hey, I am text'

This example is really silly, but you see where we’re going:

这个例子确实很愚蠢,但是您会看到我们要去的地方:

  1. Take your functions and copy them over to a separate file接受您的功能并将其复制到单独的文件中
  2. Import them and call them as normal.导入它们并正常调用它们。

When things get complicated, though, you’ll have to pass in a bunch of stuff to those functions — objects, functions for manipulating state, and so on. Today I ran into a problem where I wanted to extract three functions out of a component and they all required the same inputs (a resource and a function to update the resource). There’s got to be a better way…

但是,当事情变得复杂时,您必须将大量东西传递给这些函数-对象,用于操纵状态的函数,等等。 今天,我遇到了一个问题,我想从一个组件中提取三个函数,它们都需要相同的输入(一个resource和一个用于更新resource的函数)。 一定有更好的方法……

用类重构 (Refactoring with a class)

I made a big demo for this post. You can see the code on Github. The initial commit shows all of the functionality inside the main component (App.js) and the subsequent commits refactor the code to use a class.

我为这篇文章做了一个大型演示。 您可以在Github上查看代码。 初始提交显示了主要组件( App.js )内的所有功能,随后的提交将代码重构为使用类。

You can run this yourself and do whatever you want to it. Remember to yarn install.

您可以自己运行它,并做任何您想做的事情。 切记要yarn install

We start with a component that “fetches” an object (mimicking the way we might do this from an API) with certain attributes on it: repeat (number of boxes), side (height and width), text, color. We then have a number of ways we manipulate the view — changing the color, updating the text, and so on. After each change, we display a message.

我们从一个组件开始,该组件“获取”一个对象(模仿我们从API进行操作的方式),并带有某些属性:重复(框数),侧面(高度和宽度),文本,颜色。 然后,我们可以通过多种方式来操作视图-更改颜色,更新文本等。 每次更改后,我们都会显示一条消息。

For instance, here’s our change width and height method:

例如,这是我们的更改宽度和高度方法:

changeSide = side => {const obj = {...this.state.obj, side}this.fetchObject(obj);this.setState({ message: `You changed the sides to ${side} pixels!` });
}

We might have a number of other methods that require similar actions — or perhaps very different methods. We might start thinking about extracting this code to a helper. Then we would create a different method to call the setState action and we’d have to pass it, this.fetchObject, the object in state, and the side we are getting as an argument to the method. If we have several similar methods, that’s a whole lot of passing parameters and maybe it’s not actually that helpful (or readable).

我们可能还有许多其他方法需要类似的操作,或者可能是非常不同的方法。 我们可能会开始考虑将这些代码提取给助手。 然后,我们将创建一个不同的方法来调用setState操作,并且必须将它传递给this.fetchObject ,处于状态的对象以及作为该方法的参数要获得的side 。 如果我们有几种类似的方法,那是很多传递参数,实际上可能没有那么有用(或可读性)。

Instead we can use a class, complete with a constructor method:

相反,我们可以使用带有构造函数方法的类:

export default class ObjectManipulator {constructor( { object, fetchObject, markResettable, updateMessage, updateStateValue } ) {this.fetchObject = fetchObject;this.markResettable = markResettable;this.updateMessage = updateMessage;this.updateStateValue = updateStateValue;}changeSide = ( object, side ) => {const newObject = { ...object, side };this.fetchObject(newObject);this.updateMessage(`You changed the sides to ${side} pixels!`);this.markResettable();this.updateStateValue('side', side);};
};

This allows us to create an object whose functions we may call inside of our main component:

这使我们可以创建一个对象,可以在主要组件内部调用其功能:

const manipulator = new ObjectManipulator({object,fetchObject: this.fetchObject,markResettable: this.markResettable,updateMessage: this.updateMessage,updateStateValue: this.updateStateValue,
});

This creates an object manipulator — an instance of our ObjectManipulator class. When we call manipulator.changeSide(object, '800') it will run the changeSide method we define above. There’s no need to pass in updateMessage or any of the other methods — we pick them up from the constructor, when we created the instance.

这将创建一个对象manipulator -我们的ObjectManipulator类的实例。 当我们调用manipulator.changeSide(object, '800') ,它将运行上面定义的changeSide方法。 无需传入updateMessage或任何其他方法-创建实例时,我们从构造函数中选择它们。

You can imagine that this becomes really useful if we have a lot of these methods to deal with. In my case, I needed to call .then(res => myFunction(res) after everything I was trying to extract. Defining myFunction on the class instance instead of passing it to each function saved me a lot of code.

您可以想象,如果我们要处理许多这样的方法,那么这将变得非常有用。 以我ng myFunct ,在尝试提取所有内容后,我需要调用.then(res => myFunction(r es)。在类实例上ng myFunct而不是将其传递给每个函数可以节省很多代码。

保持一切井井有条 (Keeping everything organized)

This method of organization can be really helpful to keep everything in its place. For instance, I have an array of colors that I map over to get the color buttons you see in the example. By moving this constant into the ObjectManipulator, I can make sure it doesn’t clash with any other colors in the rest of my app:

这种组织方法对于将一切保持在原位非常有用。 例如,我映射了一组颜色以获得在示例中看到的颜色按钮。 通过将此常量移到ObjectManipulator ,我可以确保它不会与应用程序其余部分中的任何其他colors发生冲突:

export default class ObjectManipulator {[...]colors = ['blue', 'red', 'orange', 'aquamarine', 'green', 'gray', 'magenta'];
};

I can use manipulator.colors to grab the right colors for this page, whereas there might be a global colors constant that is used for something else.

我可以使用manipulator.colors来为该页面获取正确的颜色,而可能会有一个用于其他内容的全局colors常量。

参考文献 (References)

Good old Mozilla Class docs

好的老Mozilla类文档

翻译自: https://www.freecodecamp.org/news/javascript-code-cleanup-how-you-can-refactor-to-use-classes-3948118e4468/

重构javascript

重构javascript_JavaScript代码清理:如何重构以使用类相关推荐

  1. 修改软件的艺术:如何重构遗留代码

    重构是指在不改变外部行为的前提下对代码的内部结构进行重组或重新包装. 想象一下,如果你是若干年前的我,正在对经理说你要让整个团队花上两周(一个完整的迭代周期)来重构代码.经理问:"好的.你会 ...

  2. 【重构-改善代码】笔记

    长代码提炼 1.长函数切开,并把较小块的代码移至更合适的class内,降低代码重复量. 2.去除临时变量 3.重构可能引起的问题,性能问题 4.运用多态取代与价格相关的条件逻辑(switch)-- 配 ...

  3. 配电网重构matlab程序,配电网络重构matlab代码

    [实例简介] 配电网络重构matlab代码 对初学者有一定用处 [实例截图] [核心代码] 配电网络重构matlab代码 └── 配电网络重构matlab代码 ├── IEEE33 │   ├── c ...

  4. 整洁代码之道——重构

    写在前面 \\ 现在的软件系统开发难度主要在于其复杂度和规模,客户需求也不再像Winston Royce瀑布模型期望那样在系统编码前完成所有的设计满足用户软件需求.在这个信息爆炸技术日新月异的时代,需 ...

  5. java重复代码重构_重构重复代码

    java重复代码重构 As a software engineer working on a large project, you'll likely be asked to do cleanup w ...

  6. 重构笔记——代码的坏味道(上)

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42041757 在重构入门篇中,简单地介绍了重构的定义.为何 ...

  7. 一个加班多新人多团队,我们的代码问题与重构

    " 写这个文章是因为前段时间确实因为公司的业务开发太忙太紧,所有开发都处在于加班赶项目,并且加入的新人较多造成了一系列代码不可控的质量问题. 文章针对这段时间代码出现的各种各样的问题进行了一 ...

  8. 【重构】 代码的坏味道总结 Bad Smell (一) (重复代码 | 过长函数 | 过大的类 | 过长参数列 | 发散式变化 | 霰弹式修改)

    膜拜下 Martin Fowler 大神 , 开始学习 圣经 重构-改善既有代码设计 . 代码的坏味道就意味着需要重构, 对代码的坏味道了然于心是重构的比要前提; . 作者 : 万境绝尘 转载请注明出 ...

  9. 代码重构(五):继承关系重构规则

    陆陆续续的发表了多篇关于重构的文章了,还是那句话,重构是一个项目迭代开发中必不可少的一个阶段.其实重构伴随着你的项目的整个阶段.在前几篇关于重构的文章中我们谈到了函数的重构.类的重构.数据的重构以及条 ...

最新文章

  1. wamp配置虚拟域名
  2. 获取java hashCode分布
  3. 各计算机语言之父,四大编程语言之父
  4. java在线阅读word_java在线预览txt、word、ppt、execel,pdf代码
  5. GPIO代码使用流程(伪代码部分示例)
  6. caffe教程翻译:Alex’s CIFAR-10 tutorial, Caffe style
  7. Tensorflow Lite 编译
  8. linux中postscript如何生成,【转载】如何为Linux生成和打上patch
  9. 常用JS库源码 - store.js源码/underscore.js源码
  10. Django学习笔记之二
  11. 高精度事件计时器怎么关闭_Node.js 事件循环
  12. nodejs应用在linux服务器中的部署
  13. 笔记本无线网卡变身热点供手机Wi-Fi免费通过宽带上网
  14. 现代通信原理2.2:信号时间平均算子与信号物理参数
  15. mfc动态改变clip风格_欧式古典家具风格的演变历程
  16. RL之MAB:多臂老虎机Multi-Arm Bandit的简介、应用、经典案例之详细攻略
  17. Fragment already added解决
  18. maven 光速入门攻略01
  19. 金融量化数据接口API汇总
  20. 如何使用groupby函数对数据进行分组(1)

热门文章

  1. 【设计模式】设计模式C++编程实现之单例模式(Singleton Pattern)
  2. 窗体常用属性的演练 c#
  3. request-爬取一张图片的练习-答案-私
  4. 爬虫-09-get请求发起-响应对象的属性了解-练习图片的爬取
  5. linux-命令模式-光标定位-编辑中20.22
  6. OCI runtime exec failed: exec failed:解决方法
  7. Linux完全删除用户
  8. 【转】STM32中的抢占优先级、响应优先级概念
  9. 轻松带你走进shiro的世界
  10. Privacy Policy