如何使用vue module.exportss

6359人阅读
Nodejs(16)
module.exports :&
举个栗子:
module.exports = ['aaa',18]
var a= require('a')
console.log(a[1]) //输出18
module.exports =function(){
this.show=function(){
console.log('hahah~');
var a= require('a');
var obj = new a();
obj&.show();//输出hahah~
module.exports &我的理解是:你把什么东西赋给了module.exports,require后就会得到什么东西
exports :&
exports.show =function(){
console.log('hahah~');
var a= require('a');
a.show();//输出hahah~
exports已经是一个对象,你可以向这个对象里面添加属性,在require后就得到的是这个exports对象。但是你不能给exports赋一个新对象,比如exports={}
还需要注意的是如果module.exports已经有内容了,那么exports的所有操作都会失效,切记
再说一下prototype,prototype是干什么用的呢,它是在原型中添加属性,原型就像c++中的父类一样,我再来举个栗子
module.exports =function(){
module.exports.prototype.show = function(){
console.log('hahah~');
var a= require('a');
var obj = new a()
obj.show()//输出hahah~
最后,说一下类方法,说到类,那肯定是用module.exports了。栗子在此
module.exports =function(){
module.exports.show = function(){
console.log('hahah~');
var a= require('a');
a.show()//输出hahah~
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:53867次
排名:千里之外
原创:33篇
转载:32篇
(1)(1)(4)(4)(7)(1)(3)(1)(6)(2)(1)(4)(3)(14)(14)
(window.slotbydup = window.slotbydup || []).push({
id: '4740887',
container: s,
size: '250,250',
display: 'inlay-fix'NodeJS中module.exports与exports的区别
node.js通过实现CommonJS的Modules/1.0标准引入了模块(module)概念,一个模块可以通过module.exports或exports将函数、变量等导出,以使其它Script脚本通过require()函数引入并使用。
现在有一个问题:到底应该用module.exports还是用exports呢?
[javascript] &
console.log(module.exports === exports); &&
代码比较,其实两者是一样的
node在执行的时候,实际上是有一个全局变量module,但是,exports却是通过动态生成一个function,作为此function的参数传进来的。
那么exports = function() {};之后,只是地址改变。exports的内容并没有改变
那么,如果要实现类似于var assert = require(&assert&); assert();这样的代码如何实现?看以下代码:
moduleTest.js
[javascript] &
var exports = module.exports = function(param) { & &
& & console.log(param); & &
exports.print = function(param) { & &
& & console.log(param + &,we are printing.&); & &
&simple.js
[javascript] &
var test = require(&./moduleTest&); & &
test(&I'm John.&); & &
test.print(&444&); & &
So...如果要实现assert的调用,必须
[javascript]&
exports = module.exports = someO & &
exports.sth = // 如果还需要加入其他属性则这样写 &Node.js 系列之 —— module.exports 与 exports | Ghost中文网如何编译module.exports,使其在浏览器端运行? - 知乎5被浏览564分享邀请回答23 条评论分享收藏感谢收起/island205/browserify-loader ,为了让 module.exports 能够运行在浏览器端,必须处理下面这两方面的事情:支持 module.exports 的运行时模块间的依赖1添加评论分享收藏感谢收起

我要回帖

更多关于 module.exports 用法 的文章

 

随机推荐