Javascript Underscore.js 和 jQuery 相辅相成吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8907373/
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-08-24 08:02:15  来源:igfitidea点击:

Do Underscore.js and jQuery complement each other?

javascriptjqueryunderscore.js

提问by helpermethod

I'm just starting to learn JavaScript, and stumbled over Underscore.jsand jQuery. Underscore looks really cool but I wonder if jQuery does not already provide functions similar to Underscore. So, is it worthwhile to use both?

我刚刚开始学习 JavaScript,并偶然发现了Underscore.jsjQuery。Underscore 看起来很酷,但我想知道 jQuery 是否还没有提供类似于 Underscore 的功能。那么,是否值得同时使用?

采纳答案by Gazler

Taken from the underscoresite:

取自下划线网站:

It's the tie to go along with jQuery's tux, and Backbone.js's suspenders.

这是与 jQuery 的晚礼服和 Backbone.js 的吊带裙搭配的纽带。

Underscore is more suited to data manipulation and brings many Ruby methods to JavaScript. There is some crossover, but not enough to make them mutually exclusive.

Underscore 更适合数据操作,并为 JavaScript 带来了许多 Ruby 方法。有一些交叉,但不足以使它们相互排斥。

回答by rkw

  • jQuery will take care of most of your dom manipulation
  • backbone.js will help you organize all of your code and give your js application some structure (mvc pattern)
  • underscore.js will give you really useful low-level utility. I would have never needed this library until I really got into js apps (it's also a requirement for backbone.js)
  • jQuery 会处理你的大部分 dom 操作
  • Backbone.js 将帮助您组织所有代码并为您的 js 应用程序提供一些结构(mvc 模式)
  • underscore.js 将为您提供非常有用的低级实用程序。在我真正进入 js 应用程序之前,我永远不会需要这个库(这也是backbone.js 的要求)

回答by Umesh Patil

Underscore provides a total of 60 functions for processing data/code. It is agreed that many of the functionalities are present in other libraries like jQuery, Prototype or script.aculo.us. For example, functions like each, map, find, filter or toArray are present in jQuery. These are in Underscore also. This is to make the library independent of jQuery.

Underscore 总共提供了 60 个函数来处理数据/代码。许多功能都存在于其他库中,例如 jQuery、Prototype 或 script.aculo.us。例如,jQuery 中存在诸如 each、map、find、filter 或 toArray 之类的函数。这些也在下划线中。这是为了使库独立于 jQuery。

When to use Underscore?:

什么时候使用下划线?

Currently, it is widely used with Backbone.js to use MVC architecture for creating a one page Javascript Web app. The most significant ability of underscore.js is the ability of templating (which jQuery can not do). This library has many other useful functions, which are independent of other Javascript libraries.

目前,它广泛与 Backbone.js 一起使用,以使用 MVC 架构来创建单页 Javascript Web 应用程序。underscore.js 最重要的能力是模板化能力(jQuery 做不到)。这个库还有许多其他有用的函数,它们独立于其他 Javascript 库。

Find below a list of functions provided by Underscore:

在下面找到 Underscore 提供的函数列表:

Collections:each,map,reduce,reduceRight,find,filter,reject,all,any,include,invoke,pluck,max,min,sortBy,groupBy,sortedIndex,shuffle,toArray,size

集合:每个、地图、减少、减少权利、查找、过滤器、拒绝、所有、任何、包括、调用、提取、最大值、最小值、排序依据、分组依据、排序索引、随机播放、toArray、大小

Array:first,initial,last,rest,compact,flatten,without,union,intersection,difference,uniq,zip,indexOf,lastIndexOf,range

数组:第一个,初始,最后,休息,紧凑,展平,没有,联合,交集,差异,uniq,zip,indexOf,lastIndexOf,范围

Function:bind,bindAll,memoize,delay,defer,throttle,debounce,once,after,wrap,compose

功能:绑定、绑定、记忆、延迟、延迟、节流、去抖动、一次、后、包裹、组合

Object:keys,values,functions,extend,defaults,clone,tap,isEqual,isEmpty,isElement,isArray,isArguments,isFunction,isString,isNumber,isBoolean,isDate,isRegExp,isNaN,isNull,isUndefined

对象:键、值、函数、扩展、默认值、克隆、tap、isEqual、isEmpty、isElement、isArray、isArguments、isFunction、isString、isNumber、isBoolean、isDate、isRegExp、isNaN、isNull、isUndefined

Utitity:noConflict,identity,times,mixin,uniqueId,escape,template

实用程序:无冲突、身份、时间、混合、唯一标识、转义、模板

回答by DigitalDesignDj

If you are concerned about overlap, and do not need things like JQuery AJAX, then you might consider using just the JQuery selector engine, named Sizzle.

如果您担心重叠,并且不需要像 JQuery AJAX 这样的东西,那么您可以考虑只使用名为 Sizzle 的 JQuery 选择器引擎。

http://sizzlejs.com/

http://sizzlejs.com/

Note that this is not for all projects, by using Sizzle and Underscore you will lose some JQuery functionality (like AJAX), you need to consider what you really need for your specific application.

请注意,这并非适用于所有项目,通过使用 Sizzle 和 Underscore,您将失去一些 JQuery 功能(如 AJAX),您需要考虑您的特定应用程序真正需要什么。