最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using jQuery with node.js - Stack Overflow

programmeradmin2浏览0评论

I have found out that you can use jQuery with node.js but all examples are for DOM and HTML manipulation.

Do you think using it for array iterations ( for each ) etc would be ok or a bit overkill?

I have found out that you can use jQuery with node.js but all examples are for DOM and HTML manipulation.

Do you think using it for array iterations ( for each ) etc would be ok or a bit overkill?

Share Improve this question asked Sep 30, 2011 at 18:53 JonJon 40k87 gold badges242 silver badges393 bronze badges 0
Add a comment  | 

5 Answers 5

Reset to default 14

Most of the common jQuery utilities are already in implemented in the V8 engine, which node.js is built on.

for example, compare:

  • $.each with Array::forEach
  • $.map with Array::map
  • $.inArray with Array::indexOf
  • $.trim with String::trim

One of the best things about node.js is that most of the ES5 spec is already there.

Underscore.js is a much smaller utility library for manipulating objects.

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. See keeganwatkins' answer.

NodeJS already has all the EcmaScript 5 Array Extras builtin. 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.

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 -

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

发布评论

评论列表(0)

  1. 暂无评论