接上一篇走着,今天去看看module对象吧,同样使用webstrom打开前两天的项目。
建立moulde3.js文件 在里面写上如下代码:
console.log("最喜欢写代码了");
var aaa="萌萌哒";
function Person(name,age) {
this.name=name;
this.age=age;
}
Person.prototype.speak=function () {
console.log("我是"+this.name+',今年'+this.age+"岁,"+aaa+"!");
}
var p=new Person('xiaowang',18);
p.speak();
console.log(module);
运行module3.js,结果如下:
从控制台可以看见Module对象包含id,exports,parent,filename,loaded,children以及paths几个属性。
就简单说明以下吧(来自官网)
1.module.id(模块的标识符。 通常是完全解析后的文件名。)
2.module.loaded(模块是否已经加载完成,或正在加载中。)
3.module.parent(最先引用该模块的模块。)
4.module.paths(模块的搜索路径。)
5.module.exports(模块的文件级别作用域内有效的作用同module.exports)
6.module.children(被该模块引用的模块对象。)
为了更直观的看见效果,我们修改module3.js如下
console.log("最喜欢写代码了");
var aaa="萌萌哒";
function Person(name,age) {
this.name=name;
this.age=age;
}
Person.prototype.speak=function () {
console.log("我是"+this.name+',今年'+this.age+"岁,"+aaa+"!");
}
var p=new Person('xiaowang',18);
p.speak();
console.log(module);
module.exports.Person=p;
exports.aaa=aaa;
然后在同级目录下建立module4.js.结构如下:
在里面写上如下代码:
var module3=require('./module3.js');
console.log(module3);
console.log(module);
运行起来,效果如下:
"D:\java\WebStorm 2017.3.1\bin\runnerw.exe" D:\java\node\node.exe E:\javaRepository\webstrom\test\module\module4.js
最喜欢写代码了
我是xiaowang,今年18岁,萌萌哒!
Module {
id: 'E:\\javaRepository\\webstrom\\test\\module\\module3.js',
exports: {},
parent:
Module {
id: '.',
exports: {},
parent: null,
filename: 'E:\\javaRepository\\webstrom\\test\\module\\module4.js',
loaded: false,
children: [ [Circular] ],
paths:
[ 'E:\\javaRepository\\webstrom\\test\\module\\node_modules',
'E:\\javaRepository\\webstrom\\test\\node_modules',
'E:\\javaRepository\\webstrom\\node_modules',
'E:\\javaRepository\\node_modules',
'E:\\node_modules' ] },
filename: 'E:\\javaRepository\\webstrom\\test\\module\\module3.js',
loaded: false,
children: [],
paths:
[ 'E:\\javaRepository\\webstrom\\test\\module\\node_modules',
'E:\\javaRepository\\webstrom\\test\\node_modules',
'E:\\javaRepository\\webstrom\\node_modules',
'E:\\javaRepository\\node_modules',
'E:\\node_modules' ] }
/******************下面是来自于module4.js的打印****************************/
{ Person: Person { name: 'xiaowang', age: 18 }, aaa: '萌萌哒' }
Module {
id: '.',
exports: {},
parent: null,
filename: 'E:\\javaRepository\\webstrom\\test\\module\\module4.js',
loaded: false,
children:
[ Module {
id: 'E:\\javaRepository\\webstrom\\test\\module\\module3.js',
exports: [Object],
parent: [Circular],
filename: 'E:\\javaRepository\\webstrom\\test\\module\\module3.js',
loaded: true,
children: [],
paths: [Array] } ],
paths:
[ 'E:\\javaRepository\\webstrom\\test\\module\\node_modules',
'E:\\javaRepository\\webstrom\\test\\node_modules',
'E:\\javaRepository\\webstrom\\node_modules',
'E:\\javaRepository\\node_modules',
'E:\\node_modules' ] }
Process finished with exit code 0
从这段打印可以看出,module.exports与exports有一样的效果呢,其次能够更直观看出来require引入的返返回值就是在module3.js中使用exports或者module.exports声明的内容。以及通过console.log(module);能够更清楚看清module3.js与module4.js之间的依赖关系。
好了,module就暂时介绍到这里了。接下来介绍global对象中的process对象。以及全局process对象。