A classic interview question for nodejs
this.d = 4;
exports.c = 3;
module.exports = {
a: 1,
b: 2
}
//module.exports.a = 1
//module.exports.b = 2
const a = require('./a.js')
console.log(a);
The result of execution here is:
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
this.d = 4;
exports.c = 3;
//module.exports = {
// a: 1,
// b: 2
// }
module.exports.a = 1
module.exports.b = 2
const a = require('./a.js')
console.log(a);
The result of the execution here is:
here only the two output methods are different, which leads to different results. The reason is related to the underlying code of node:
because require will go through the following steps when it is executed:
Note that the following codes are all The theoretical code cannot be executed, it is only used for analysis
1. The resolve method is called and spliced into an absolute path
function require(modulePath){
//模块路径
}
2. In the cache section, determine whether the module has a cache. cache is the cache area of the require function
if(require.cache["C:\\Users\\admin\\Desktop\\0315nodejs\\04.js"]){
return require.cache["C:\\Users\\admin\\Desktop\\0315nodejs\\04.js"]
}
3. Read the content of the target file
4. Wrap it into a function
function require(modulePath){
//模块路径
if(require.cache["C:\\Users\\admin\\Desktop\\0315nodejs\\04.js"]){
return require.cache["C:\\Users\\admin\\Desktop\\0315nodejs\\04.js"]
}
function _temp(module,exports,require,__dirname,__filename){
//在require目标对象的所有代码都在这个内部函数执行
this.d = 4;
exports.c = 3;
module.exports = {
a : 1,
b : 2
}
}
}
5. Create a module.exports object (emphasis)
function require(modulePath){
//模块路径
if(require.cache["C:\\Users\\admin\\Desktop\\0315nodejs\\04.js"]){
return require.cache["C:\\Users\\admin\\Desktop\\0315nodejs\\04.js"]
}
function _temp(module,exports,require,__dirname,__filename){
//在require目标对象的所有代码都在这个内部函数执行
this.d = 4;
exports.c = 3;
module.exports = {
a : 1,
b : 2
}
}
// 5.创建module.exports对象
module.exports = {
}
const exports = module.exports
_temp.call(module.exports,exports,require,__dirname,__filename)
return module.exports;
}
When executing the internal function _temp, it will change the point of this through call, point it to module.exports and create a module.exports object, then create exports and assign module.exports to exports, that is, now this, module.exports, exports is actually a thing:
console.log(module.exports === exports)//true
console.log(module.exports === this)//true
Finally return to module.exports;
so
this.d = 4;
exports.c = 3;
module.exports.a = 1
module.exports.b = 2
The output {d:4, c:3, a:1, b:2} is because they are all adding things to module.exports;
but
this.d = 4;
exports.c = 3;
module.exports = {
a: 1,
b: 2
}
There is a problem here. At the beginning, an empty object of module.exports was created, but now a new object is assigned. This leads to module.exports pointing to a new address.
Here I draw a picture to show the
first case: the
second case:
and the require function returns module.exports, which naturally outputs {a:1, b:2)