node基础再现--module.exports 和exports
实际上,最最基础的方法,最最原始的方法是module.exports,至于exports,是为了方便书写才出来的,应该说,module.exports 包含exports,所工作的范围更加的广泛!
module的用法:(可以暴露任何部位,任何形式的值)
(后来坑之处)require()中的是文件名,而不是什么方法,所以,就简单理解了;
引入的文件当中,导出了什么,就有什么方法,
//此文件为modle.js
//相当于类,类名和文件名相同最好!
var modle = {
var a = function(){};
var b = function(){};
}
//module one暴露所有的方法,此时,无法再暴露其他方法
module.exports = modle;
//module two 暴露些许方法
module.exports.output = modle.a;
//新开辟单元暴露方法
module.exports.fuck = function(){console.log('this is newwe!')}
假若我引入了modle.js如下
var modle = require('./modle');
注意,上面require进来的是module.exports这个对象!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//若是module.exports = modle,即:这个文件仅仅暴露了对象modle,引入后赋给modle,使用如下:
modle.a();modle.b();
//若是module.exports.output = modle;那么是将modle对象赋给了module.exports的output,然后require进来了module.exports对象,使用如下:
modle.modle.a() modle.modle.b()
//至于exports,只能够exports.什么什么,也就是只能给他创建对象,然后,暴露,但是不能exports=遵循了这点以后实际和module.exports.hello = function(){}一样
至于exports:
它是有上面的引升来的,等于上面那个,所以,它不能在赋值了,只能新开辟值,只能新开辟值,只能新开辟值,只能新开辟值(重要)
exports.hello = function(){console.log('hello');}
注:如果实在是分不清两者的区别,那么,放弃exports,仅仅使用module.exports足够了!
node基础再现--module.exports 和exports的更多相关文章
- es6 import export 与 node 中的module.exports exports
1.export a.export 变量 export var name = 'jack';export var age = 18;//等同于 var name = 'jack';var age = ...
- Node.js module.exports和exports的区别
require 用来加载代码,而 exports 和 module.exports 则用来导出代码,从接触node.js就不会它们两陌生,上代码: foo.js exports.a = functio ...
- Node.js模块导出module.exports 和 exports,Es6模块导出export 和export default的区别
1.module.exports module变量代表当前模块.这个变量是一个对象,module对象会创建一个叫exports的属性,这个属性的默认值是一个空的对象: module.exports ...
- node (02 CommonJs 和 Nodejs 中自定义模块)顺便讲讲module.exports和exports的区别 dependencies 与 devDependencies 之间的区别
CommonJS 规范的提出,主要是为了弥补当前 JavaScript 没有标准的缺陷.它的终极目标就是:提供一个类似 Python,Ruby 和 Java 语言的标准库,而不只是停留在小脚本程序的阶 ...
- (转)Node.js module.exports与exports
本文转自Node.js module.exports与exports 作者: chemdemo 折腾Node.js有些日子了,下面将陆陆续续记录下使用Node.js的一些细节. 熟悉Node.js的童 ...
- module.exports和exports得区别
对module.exports和exports的一些理解 可能是有史以来最简单通俗易懂的有关Module.exports和exports区别的文章了. exports = module.exports ...
- module中module.exports与exports的区别(转)
转https://cnodejs.org/topic/55ccace5b25bd72150842c0a require 用来加载代码,而 exports 和 module.exports 则用来导出代 ...
- nodejs里的module.exports和exports的关系
关于node里面的module.exports和exports的异同,网上已经有很多的资料,很多的文章,很多的博客,看了很多,好像懂了,又好像不懂,过几天又不懂了...大致总结是这样的: //下面这种 ...
- module.exports与exports,export和export default
还在为module.exports.exports.export和export default,import和require区别与联系发愁吗,这一篇基本就够了! 一.首先搞清楚一个基本问题: modu ...
随机推荐
- struts2 拦截器1
action invoke前会调用,invoke后会调用 public class FirstInterceptor extends AbstractInterceptor{ @Override pu ...
- bzoj 3751: [NOIP2014]解方程 同余系枚举
3.解方程(equation.cpp/c/pas)[问题描述]已知多项式方程:a ! + a ! x + a ! x ! + ⋯ + a ! x ! = 0求这个方程在[1, m]内的整数解(n 和 ...
- BZOJ 2226 LCMSum
Description Given \(n\), calculate the sum \(LCM(1,n) + LCM(2,n) + \cdots + LCM(n,n)\), where \(LCM( ...
- MVC自学系列之一(MVC入门篇)
MVC是如何适用于ASP.NET中的 自从2002年ASP.NET 1.0版本的首次发布,就知道ASP.NET和Web Forms是同样一回事.ASP.NET由抽象两层类所支持: -->Sy ...
- python模块与包加载机制
模块的搜索路径: When a module named spam is imported, the interpreter searches for a file named spam.py in ...
- Android网络框架Volley(实战篇)
之前讲了ym—— Android网络框架Volley(体验篇),大家应该了解了volley的使用,接下来我们要看看如何把volley使用到实战项目里面,我们先考虑下一些问题: 从上一篇来看 mQu ...
- [LeetCode#252] Meeting Rooms
Problem: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2] ...
- Android Fragment实现分屏
在项目中碰到一个问题,新开发一个平板APP,项目要求是把原来的一个手机端APP放在项目左侧显示,右侧添加新加的功能. 首先想到了Fragment,以前做过Fragment的一些简单的Demo,但是都没 ...
- hdu 4432 Sum of divisors(十进制转其他进制)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4432 代码: #include<cstdio> #include<cstring&g ...
- TOMCAT启动完成但是ECLIPSE仍然显示starting....
最近重新部署了一个TOMCAT服务,但是启动碰到个问题,虽然TOMCAT控制台已显示启动成功,但是ECLIPSE右下角仍然一直显示STARTING,最后TOMCAT超时,启动失败. 之前以为是拷贝工程 ...