简体   繁体   English

在node.js中使用jQuery

[英]Using jQuery with node.js

I have found out that you can use jQuery with node.js but all examples are for DOM and HTML manipulation. 我发现你可以在node.js中使用jQuery,但所有的例子都是用于DOM和HTML操作。

Do you think using it for array iterations ( for each ) etc would be ok or a bit overkill? 您是否认为将它用于数组迭代(对于每个)等都可以,或者有点矫枉过正?

Most of the common jQuery utilities are already in implemented in the V8 engine, which node.js is built on. 大多数常见的jQuery实用程序已经在V8引擎中实现,其中构建了node.js。

for example, compare: 例如,比较:

One of the best things about node.js is that most of the ES5 spec is already there. node.js的最好的一点是,大多数ES5规范已经存在。

Underscore.js is a much smaller utility library for manipulating objects. Underscore.js是一个小得多的实用程序库,用于操作对象。

http://documentcloud.github.com/underscore/ http://documentcloud.github.com/underscore/

npm install underscore

EDIT: 编辑:

However, node.js has much better support for ES5 than browsers, and it's likely that you may not even need a library for manipulating objects. 但是,node.js对ES5的支持要比浏览器好得多,而且你可能甚至不需要一个库来操作对象。 See keeganwatkins' answer. 见keeganwatkins的回答。

NodeJS already has all the EcmaScript 5 Array Extras builtin. NodeJS已经内置了所有的EcmaScript 5 Array Extras。 For example if you want all the odd squares: 例如,如果你想要所有的奇数方块:

[1,2,3,4,5,6,7,8,9].map(function(n){
    return n*n;
}).filter(function(n){
    return n%2;
});
//-> [1, 9, 25, 49, 81]

If you would like to see the other methods on the arrays, you can go to my JavaScript Array Cheat Sheet . 如果您想在阵列上看到其他方法,可以转到我的JavaScript数组备忘单

If you want the sum of all the cubes, you can: 如果你想要所有立方体的总和,你可以:

[1,2,3,4,5,6,7,8,9].map(function(n){
    return n * n * n;
}).reduce(function(p, n){
    return p + n;
});
//-> 2025

jQuery has some nifty non-DOM features you can borrow - jQuery有一些漂亮的非DOM功能,你可以借用 -

https://github.com/jquery/jquery/tree/master/src https://github.com/jquery/jquery/tree/master/src

Just strip out what you do not need (like a reference to window). 只需去掉你不需要的东西(比如对窗口的引用)。

If there are just a few functions you want to add, an alternative to adding a full library is to just implement the functions you want: 如果只想添加一些函数,添加完整库的替代方法就是实现所需的函数:

Many functions that are partially supported on some browsers are documented in MDN with compatibility code that can be used to add the function: Array.forEach MDN中记录了某些浏览器部分支持的许多函数,其中包含可用于添加函数的兼容性代码: Array.forEach

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM