开发者

What is exports and prototype in Javascript?

I am new to Javasc开发者_如何学编程ript 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;


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜