什么是Javascript中的导出和原型? [英] What is exports and prototype in Javascript?
问题描述
我是Javascript的新手,并且在我阅读的代码中看到了很多导出和原型的用法。它们主要用于什么以及它们如何工作?
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;
推荐答案
导出用于使模块的某些部分可用于模块外部的脚本。所以当有人在另一个脚本中使用 require
时,如下所示:
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");
他们可以访问您在 module.exports中放置的任何函数或属性
在您的示例中理解原型的最简单方法是 Server
是一个继承的类 HTTPSServer
的所有方法。 prototype
是在javascript中实现类继承的一种方法。
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.
这篇关于什么是Javascript中的导出和原型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!