How to use Prototype.js with Node.js?

I want to use some functions for server-side javascript. I think the prototype checks the browser type, but of course node.js is not a browser. I get the following error:

$ node > require('./prototype') ; ReferenceError: navigator is not defined at /home/guest/projects/javascript/prototype.js:14:5 at Object.<anonymous> (/home/guest/projects/javascript/prototype.js:23:4) at Module._compile (node.js:462:23) at Module._loadScriptSync (node.js:469:10) at Module.loadSync (node.js:338:12) at loadModule (node.js:283:14) at require (node.js:411:14) at cwdRequire (repl:29:10) at [object Context]:1:1 at Interface.<anonymous> (repl:96:19) 

prototype.js - version 1.7, node.js - version 0.2.6

+6
prototypejs
source share
4 answers

The prototype is written as modular. This means that you can only use the useful parts that extend the Array and the class and function (I like these bits!), And leave the parts that deal with the browser and DOM (bits that are slow in IE and don't exist in node) .

Let's start with https://github.com/sstephenson/prototype . Then select the necessary parts from src/prototype/ and src/prototype/lang/ .

I wish you good luck in such an exciting task.

+6
source share

Late answer, but I'm sure it might be useful to some people:

https://github.com/Rixius/prototype.node.js

A few days ago I did something similar, and realized that it was already done ... This repo is hard to find even when searching for github.

+4
source share

If you look at the source of Prototype.js, it is closely related to the browser environment, which is not provided by node (since it is not a web browser).

jsdom is trying to make fun of the browser environment and successfully used, run jQuery on the server side. Your mileage may vary.

+2
source share

There is underscore.js specifically for node.js that implements most prototypes of your favorite functions:

 Underscore is a utility-belt library for JavaScript that provides a lot of the functional programming support that you would expect in Prototype.js. 

It is faster than the prototype itself because it does not extend any of the built-in JavaScript objects.
In this regard, the syntax is slightly different:

 // prototype.js: anArray.each(function(){ ... }); // underscore.js: _ = require('underscore'); _.each(anArray, function(){ ... }); 

If you are looking for String prototypes such as trim , look underscore.string

0
source share

All Articles