什么是 Javascript 中的导出和原型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5380159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is exports and prototype in Javascript?
提问by Kiran Ryali
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?
我是 Javascript 的新手,并且在我阅读的代码中看到了大量使用导出和原型的情况。它们主要用于什么以及它们如何工作?
//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;
回答by Tom Gruner
Exportsis used to make parts of your module available to scripts outside the module. So when someone uses require
like below in another script:
导出用于使模块的某些部分可用于模块外的脚本。因此,当有人require
在另一个脚本中使用如下所示时:
var sys = require("sys");
They can access any functions or properties you put in module.exports
他们可以访问您输入的任何功能或属性 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.
理解示例中原型的最简单方法是,它Server
是一个继承HTTPSServer
. prototype
是在 javascript 中实现类继承的一种方法。