如何使用JavaScript中的工厂函数构建可靠的对象

Discover Functional JavaScript was named one of the best new Functional Programming books by BookAuthority!

“发现功能JavaScript”BookAuthority评为最佳新功能编程书籍之一

I suggest to take into consideration these ideas for building reliable objects in JavaScript:

我建议考虑以下想法以在JavaScript中构建可靠的对象:

  • Split objects in two: data objects and behavior objects

    将对象一分为二:数据对象和行为对象
  • Make the data objects immutable

    使数据对象不可变
  • Expose behavior and hide data in behavior objects

    公开行为并隐藏行为对象中的数据
  • Build testable behavior objects

    建立可测试的行为对象

数据与行为对象 (Data vs Behavior Objects)

Essentially there are two kinds of objects in an application:

本质上,应用程序中有两种对象:

  • Data Objects — expose data

    数据对象-公开数据

  • Behavior Objects — expose behavior and hide data

    行为对象-公开行为并隐藏数据

数据对象 (Data Objects)

Data objects expose data. They are used to structure and transfer data inside the application.

数据对象公开数据。 它们用于在应用程序内部构造和传输数据。

Let’s take the case of a to-do list application.

让我们以待办事项清单应用程序为例。

This is how the to-do data object, gotten from the server, may look:

这是从服务器获得的待办事项数据对象的外观:

{ id: 1, title: "This is a title", userId: 10, completed: false }

And this is how a data object used to display information in the view may look:

这是用来在视图中显示信息的数据对象的外观:

{ id: 1, title: "This is a title", userName: "Cristi", completed: false };

As you can see, both objects contain only data. There is a small difference between them: the data object for the view has userName instead of the userId.

如您所见,两个对象仅包含数据。 它们之间的差别很小:视图的数据对象具有userName而不是userId

Data objects are plain objects, usually built with object literals.

数据对象是普通对象,通常使用对象文字构建。

行为对象 (Behavior Objects)

Behavior objects expose methods and hide data.

行为对象公开方法并隐藏数据。

Behavior objects act on data objects. They may take data objects as inputs or return data objects.

行为对象作用于数据对象。 它们可以将数据对象作为输入或返回数据对象。

I’ll take the case of the TodoStore object. The responsibility of the object is to store and manage the list of to-dos. It makes the synchronization with the server using the dataService object.

我将以TodoStore对象TodoStore 。 对象的职责是存储和管理待办事项清单。 它使用dataService对象与服务器进行同步。

Read Functional Architecture with React and Redux and learn how to build apps in function style.

阅读具有React和Redux的功能架构,并学习如何以函数样式构建应用程序。

Discover Functional JavaScript was named one of the best new Functional Programming books by BookAuthority!

发现功能JavaScript被称为 BookAuthority最好的新功能编程书籍

For more on applying functional programming techniques in React take a look at Functional React.

有关在React中应用函数式编程技术的更多信息,请查看 Functional React

You can find me on Medium and Twitter.

您可以在MediumTwitter上找到我。

翻译自: https://www.freecodecamp.org/news/how-to-build-reliable-objects-with-factory-functions-in-javascript-9ec1c089ea6f/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值