问题描述
使用JavaScript在客户端实现业务逻辑是一个好主意吗?
Is it a good idea to implement business logic on the client side with JavaScript?
应该有什么样的逻辑?验证逻辑?与GUI相关?
What kind of logic should be there? Validation Logic? Related to GUI?
如果想要在另一个应用程序(公开)中使用相同的逻辑,如果在JavaScript中实现它将意味着您无法重用该逻辑。
What would you do if the same logic want to be used in another application (exposed) implementing it in JavaScript would mean you can't reuse that logic.
另一方面,在服务器端拥有所有逻辑将意味着对服务器的更多请求。
On the other hand having all logic on the server side would mean more requests to the server.
您如何看待?
推荐答案
您可以创建可重复使用的Javascript模块,因此在几个不同的富有的uis中没有内在障碍来恢复逻辑。但是,正如已经指出的那样,您可能最终会在JavaScript和服务器上使用的任何内容之间重复(Java,PHP ...) - 在验证的情况下,在提供高性能之间进行权衡由于重复导致的用户体验和复杂性。
You can create reusable Javascript modules so there's no intrinsic barrier to resuing logic in several different rich uis. However, as has already been pointed out, you probably end up with duplication between the JavaScript and whatever you're using on the server (Java, PHP ...) - in the case of validation that's a trade-off between giving a performant user experience and complexity due to duplication.
我可以想象你会选择复制而不仅仅是验证的场景。考虑计算总订单价值:我们真的想要进行服务器端往返吗?对列表进行排序 - 我们倾向于在JavaScript中快乐地做到这一点,但是我们排序可以获得有趣的,专门的比较器功能吗?绘制边界可能非常棘手,计算折扣和销售税?
I can imagine scenarios where you would choose to duplicate more than just validation. Consider computing a total order value: do we really want to make a server-side round trip for that? Sorting a list - we tend to do that happily in JavaScript, but we sorting can get interesting, specialised comparator functoions? Drawing the boundary may be quite tricky, computing discounts and sales tax?
最后这是一个判断电话,然后仔细了解后果。如果您复制逻辑,那么您可以设计一个确保一致性的测试策略吗?对于低容量系统,您可能倾向于支持更多服务器交互并减少重复,但您可能会针对更大或更苛刻的用户群做出不同的决策。
In the end it's a judgement call, followed by careful understanding of consequences. If you duplicate logic then can you devise a test strategy that ensures consistency? With low volume systems you may be inclined to favour more server interactions and less duplication, but you may well make different decisions for a larger or more demanding user-base.
这篇关于JavaScript中的业务逻辑。胖客户端与瘦客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!