What is exports and prototype in Javascript?

Javascriptnode.js

Javascript Problem Overview


I am new to Javascript and am seeing a lot of usage of exports and prototype in the code that I read. What are they mainly used for and how do they work?

//from express
var Server = exports = module.exports = function HTTPSServer(options, middleware){
  connect.HTTPSServer.call(this, options, []);
  this.init(middleware);
};

Server.prototype.__proto__ = connect.HTTPSServer.prototype;

Javascript Solutions


Solution 1 - Javascript

Exports is used to make parts of your module available to scripts outside the module. So when someone uses require like below in another script:

var sys = require("sys");  

They can access any functions or properties you put in module.exports

The easiest way to understand prototype in your example is that Server is a class that inherits all of the methods of HTTPSServer. prototype is one way to achieve class inheritance in javascript.

Solution 2 - Javascript

This video explains node.js module.exports and here is a resource which describes JavaScript prototype.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKiran RyaliView Question on Stackoverflow
Solution 1 - JavascriptTom GrunerView Answer on Stackoverflow
Solution 2 - Javascriptyojimbo87View Answer on Stackoverflow