登录  /  注册
js中的exports详解
小云云
发布:2023-03-22 22:16:02
原创
20791人浏览过

本文主要和大家分享js中的exports详解,主要以代码的方式和大家分享,希望能帮助到大家。

相关视频推荐:1.JavaScript极速入门_玉女心经系列
相关手册推荐:1.JavaScript中文参考手册

写法1

exports.hello = function(){
    console.log(‘world’);
}
登录后复制

写法2

var f = {
    hello : function(){
        console.log(‘world’);
    }
}
module.exports = f;
登录后复制

假设我们写的这个模块的文件名为hello.js,执行下面的代码

var h = require(‘hello’);
h.hello();
登录后复制

对于上面的两种写法,执行这段代码后得出的结果是一样的。

module.exports :

举个栗子:

1、

//a.js
module.exports = ['aaa',18]
//b.js
var a= require('a')console.log(a[1]) //输出18
登录后复制

2、

//a.js
module.exports =function(){
this.show=function(){
console.log('hahah~');
}
}
//b.js
var a= require('a');
var obj = new a();obj .show();//输出hahah~
module.exports
登录后复制

我的理解是:你把什么东西赋给了module.exports,require后就会得到什么东西

exports : 

//a.js
exports.show =function(){
console.log('hahah~');
}
//b.js
var a= require('a');
a.show();//输出hahah~
登录后复制

exports已经是一个对象,你可以向这个对象里面添加属性,在require后就得到的是这个exports对象。

但是你不能给exports赋一个新对象,比如exports={}

还需要注意的是如果module.exports已经有内容了,那么exports的所有操作都会失效,切记

再说一下prototype,prototype是干什么用的呢,它是在原型中添加属性,原型就像c++中的父类一样,我再来举个栗子
1、

//a.js
module.exports =function(){
}
module.exports.prototype.show = function(){
console.log('hahah~');
}
//b.js
var a= require('a');
var obj = new a()
obj.show()//输出hahah~
登录后复制

最后,说一下类方法,说到类,那肯定是用module.exports了。

栗子在此

1、

//a.js
module.exports =function(){
}
module.exports.show = function(){
console.log('hahah~');
}
//b.js
var a= require('a');
a.show()//输出hahah~
##module.exports与exports的区别
登录后复制

每一个node.js执行文件,都自动创建一个module对象,同时,module对象会创建一个叫exports的属性,初始化的值是 {}

module.exports = {};
Node.js为了方便地导出功能函数,node.js会自动地实现以下这个语句
foo.js
exports.a = function(){
console.log('a')
}
exports.a = 1 
test.js
var x = require('./foo');
console.log(x.a)
登录后复制

看到这里,相信大家都看到答案了,exports是引用 module.exports的值。

module.exports 被改变的时候,exports不会被改变,而模块导出的时候,真正导出的执行是module.exports,而不是exports

再看看下面例子

foo.js
exports.a = function(){
 console.log('a')
}
module.exports = {a: 2}
exports.a = 1 
test.js
var x = require('./foo');
console.log(x.a)
result:
2
登录后复制

exports在module.exports 被改变后,失效。
是不是开始有点廓然开朗,下面将会列出开源模块中,经常看到的几个使用方式。

##module.exports = View
function View(name, options) { 
  options = options || {};
  this.name = name;
  this.root = options.root;
  var engines = options.engines;
  this.defaultEngine = options.defaultEngine;
  var ext = this.ext = extname(name);
  if (!ext && !this.defaultEngine) throw new Error('No default engine was specified and no         extension was provided.');
  if (!ext) name += (ext = this.ext = ('.' != this.defaultEngine[0] ? '.' : '') +     this.defaultEngine);
  this.engine = engines[ext] || (engines[ext] = require(ext.slice(1)).__express);
  this.path = this.lookup(name);
}
module.exports = View;
登录后复制

javascript里面有一句话,函数即对象,View 是对象,module.export =View, 即相当于导出整个view对象。

外面模块调用它的时候,能够调用View的所有方法。不过需要注意,只有是View的静态方法的时候,才能够被调用,prototype创建的方法,则属于View的私有方法。

foo.js
function View(){
}
View.prototype.test = function(){
 console.log('test')
}
View.test1 = function(){
 console.log('test1')
}
module.exports = View
test.js
var x = require('./foo');
console.log(x) //{ [Function: View] test1: [Function] }
console.log(x.test) //undefined
console.log(x.test1) //[Function]
x.test1() //test1
##var app = exports = module.exports = {};
登录后复制

其实,当我们了解到原理后,不难明白这样的写法有点冗余,其实是为了保证,模块的初始化环境是干净的。同时也方便我们,即使改变了 module.exports 指向的对象后,依然能沿用 exports的特性

exports = module.exports = createApplication;
/**
 * Expose mime.
 */
exports.mime = connect.mime;
登录后复制

例子,当中module.exports = createApplication改变了module.exports了,让exports失效,通过exports = module.exports的方法,让其恢复原来的特点。

##exports.init= function(){}

这种最简单,直接就是导出模块 init的方法。

##var mongoose = module.exports = exports = new Mongoose;

集多功能一身,不过根据上文所描述的,大家应该不能得出答案。

以上就是js中的exports详解的详细内容,更多请关注php中文网其它相关文章!

相关标签:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
  • 中小型创业创意公司网站模板
  • 管道维修家政服务登录页模板
  • 儿童公益慈善机构宣传网站模板
网站特效
网站源码
网站素材
前端模板
关于我们免责申明意见反馈讲师合作广告合作技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023//m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学