简体   繁体   English

Javascript:我们应该在什么时候使用库?

[英]Javascript: At what point (if ever) should we use libraries?

Similar questions have been asked on SO, but I couldn't find this one. 在SO上也提出了类似的问题,但我找不到这个问题。 If I'm wrong, I'm sure the SO vigilantes will correct me ;) 如果我错了,我敢肯定,民警将纠正我;)

My question is (deceptively) simple: At what point, if ever, should we use Javascript libraries? 我的问题很简单(看似): 在什么时候使用JavaScript库?

Many developers advocate only using pure Javascript. 许多开发人员主张仅使用纯Javascript。 Any yet it seems most developers do use some libraries. 似乎大多数开发人员都确实使用某些库。 I'm not (necessarily) asking about specific libraries, but at what point do you decide to use a library or even a framework? 我不是(有必要)询问特定的库,但是您决定在什么时候使用库甚至框架?

For example: 例如:

If I want to display a countdown timer, it would certainly be wasteful to include libraries for a timer and for DOM manipulation, since Javascript is perfectly capable of simply handling these operations. 如果我想显示一个倒数计时器,包括计时器和DOM操作库肯定是浪费的,因为Java语言完全可以简单地处理这些操作。

BUT

If I am building a large-scale social media platform (think Facebook), surely it would be wise to at least include JQuery for cross-browser UI consistency, DOM manipulation, maybe Ajax, etc... 如果我要构建一个大型的社交媒体平台(例如Facebook),那么至少包括跨浏览器UI一致性的JQuery,DOM操作,也许是Ajax等肯定是明智的。

THEN 然后

maybe we can look at some others -- an MVC framework, a templating library, etc... 也许我们可以看看其他一些东西-MVC框架,模板库等...

I know many people also recommend only pulling the parts that you actually use out of a library. 我知道很多人也建议仅从库中拉出您实际使用的零件。

SO 所以

Any general recommendations for when (or how) to include libraries? 关于何时(或如何)包含库的任何一般性建议?

恕我直言,当您花费更多的时间来实现移动部件而不是应用程序本身时,就该开始使用库了

In general, you should use a library when you are comfortable with it. 通常,您应该在感到满意的情况下使用库。 If you use jQuery or Mootools, and include them from for example Google Hosted Libraries ( https://developers.google.com/speed/libraries/ ) or another CDN, the file itself will nearly always be cached, or served once from a highspeed CDN with 30+ day expiration. 如果您使用jQuery或Mootools,并从例如Google Hosted Libraries( https://developers.google.com/speed/libraries/ )或其他CDN中包含它们,则文件本身几乎总是会被缓存,或者从超过30天有效期的高速CDN。 While it is certainly possible to implement simple problems without using any libraries, the point of the library is to make your life as a developer easy. 虽然当然可以在不使用任何库的情况下实现简单的问题,但该库的目的是使您作为开发人员的生活变得轻松。 If your life becomes easier by using a library which does not make your site slower, or puts strain on your server, make it easier by all means. 如果使用不会降低站点速度或使服务器承受压力的库使您的生活变得更轻松,则请务必使其变得更轻松。

As a recommendation, I'd reverse the question: always use a library, unless you can just as simply and quickly implement the full required solution without it. 作为建议,我将这个问题反过来:始终使用一个库,除非您可以像没有它一样简单快速地实现所需的完整解决方案。

I don't think there's a clear answer on this, and wouldn't be surprised if the question is closed because of it. 我认为对此没有明确的答案,因此,如果问题已解决,也不会感到惊讶。 I'll offer up my views in any case. 无论如何,我都会提出自己的看法。

Your example of a counter vs. a social media platform is much too contrived to be in any way realistic. 您针对柜台与社交媒体平台的例子太过虚构,以任何方式都不现实。 But even if it was a reasonable example, I'd say application size or complexity (perceived or otherwise) has very little to do with your decision. 但是,即使这是一个合理的例子,我也会说应用程序的大小或复杂性(无论是感知的还是其他的)与您的决定关系不大。 In fact, I think you're creating a problem you very likely don't have yet. 实际上,我认为您正在制造一个很可能还没有的问题。

Instead of thinking of the tools needed to implement your application, think about what it is that you're trying to achieve with it. 不用考虑实现应用程序所需的工具,而是考虑要使用它实现的目标。 Some call this business logic, call it whatever you will, but if you're spending time thinking about tools rather then how to get to market, it's very likely you're focusing on the wrong things. 有人将这种业务逻辑称为“业务逻辑”,随便您怎么称呼它,但是如果您花时间在思考工具而不是如何推向市场,那么您很可能会专注于错误的事情。 Tools, and when to use them, should never be your primary concern. 工具以及何时使用它们,永远都不是您的主要关注点。

Here's what I think you should do. 我认为这是您应该做的。 Jot down two lines about why your application makes sense and why it's existence is justified – don't mention any tools! 写下两行关于您的应用为何有意义以及为什么它存在的理由是正确的-不要提及任何工具! Bake them in to a fortune cookie and open it a few days later. 将它们烘烤到幸运饼干中,几天后将其打开。 If you read it and it still makes sense, then figure out what the minimum feature set is – don't mention any tools! 如果您阅读它仍然有意义,那么请弄清楚最小功能集是什么–不要提及任何工具! Look at it again a couple of days later, does it still make sense? 几天后再看一次,仍然有意义吗? Excellent! 优秀的! Now build a prototype – don't care about what tools to use, just use whatever works to prove your idea. 现在建立一个原型-不在乎要使用什么工具,只要用任何可行的方法来证明您的想法即可。 Show it to someone else, is it a useful demo? 向其他人展示,这是有用的演示吗? Yes? 是? Superb, you've got a project! 棒极了,您有一个项目!

Nowhere in this does it matter whether it was your own pure javascript, polluted by libraries or whether it was a completely different environment. 不管是您自己的纯JavaScript脚本,被库污染还是完全不同的环境,这都无关紧要。 The tools are not important. 这些工具并不重要。 What's important is what you're trying to achieve and how to get it out there. 重要的是您要实现的目标以及如何实现目标。 The tools are peripheral, secondary. 这些工具是外围的,辅助的。 They are whatever you need to use to get where you're going. 它们是您到达目的地所需要的任何东西。 If that means using libraries that provide the moving parts to get your app rolling – fine! 如果那意味着要使用提供运动部件的库来使您的应用程序滚动起来–很好! If it means using coffeescript over javascript – fine! 如果这意味着通过JavaScript使用coffeescript –很好! In the end, what really matters is what you're trying to achieve, the tools you use to get there can't ever be the primary concern. 最后,真正重要的是您要实现的目标,而实现目标所用的工具永远不是主要问题。

Figure it out as you go along. 继续进行计算。 You'll probably get it wrong a bunch of times before you get it right, that's just the way the cookie crumbles. 在正确之前,您可能会多次出错,这就是Cookie崩溃的方式。


You may also find this video useful. 您可能还会发现此视频很有用。 It's a talk about clean architectures, although it's mostly a recount of how not focusing on tools helped build a better product, and is likely what you want to do. 这是关于干净架构的讨论,尽管主要是关于注重工具如何帮助构建更好的产品的回顾,这很可能是您想做的。

https://vimeo.com/43612849 https://vimeo.com/43612849

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该使用哪些语言和库来使用Gmail? - What languages and libraries should I use to work with Gmail? 我应该使用哪些库或平台来构建在客户端之间提供实时异步数据同步的Web应用程序? - what libraries or platforms should I use to build web apps that provide real-time, asynchronous data synchronization between clients? 想在Web应用程序中使用本机C库,我有哪些选择? - Want to use native C libraries in a web application, what are my options? 我应该在DroneDeploy应用中使用哪些样式? - What styles should I use in a DroneDeploy App? 我什么时候应该在JavaScript中使用MVC框架? - When should I use an MVC framework in JavaScript? 是否曾经使用过Paas开发Web应用程序? - Ever use force paas to develop web apps? 从J2EE Modern Web App制作可下载文件时,应使用哪种编码? - What encoding should be used when we make a downloadable file from a J2EE Modern Web App? 对Javascript库的查询 - Queries on Javascript Libraries API是否应该公开“批准”或“拒绝”之类的操作? - Should an API ever expose actions like “Approve” or “Deny”? 我应该使用哪些工具为我的Web应用程序创建教程? - What tools should I use to create a tutorial for my web application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM