什么是 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:56:57  来源:igfitidea点击:

What is exports and prototype in Javascript?

javascriptnode.js

提问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 requirelike 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 Serveris a class that inherits all of the methods of HTTPSServer. prototypeis one way to achieve class inheritance in javascript.

理解示例中原型的最简单方法是,它Server是一个继承HTTPSServer. prototype是在 javascript 中实现类继承的一种方法。

回答by yojimbo87

Thisvideo explains node.js module.exports and hereis a resource which describes JavaScript prototype.

视频解释了 node.js module.exports,这里是描述 JavaScript 原型的资源。