Tra*_*Liu 71 javascript this require apply node.js
我试过这个:
// mod.js
var a = 1;
this.b = 2;
exports.c = 3;
// test.js
var mod = require('./mod.js');
console.log(mod.a); // undefined
console.log(mod.b); // 2
console.log(mod.c); // 3, so this === exports?
Run Code Online (Sandbox Code Playgroud)
所以我想象require()可能是这样实现的:
var require = function (file) {
var exports = {};
var run = function (file) {
// include "file" here and run
};
run.apply(exports, [file]);
return exports;
}
Run Code Online (Sandbox Code Playgroud)
是对的吗?请帮我理解require(),或者在哪里可以找到源代码.谢谢!
Andrey展示了源代码,但是如果你也想知道如何使用它,那么简单而简单的解释就在这里(http://nodejs.org/api/modules.html).
这对我来说是两个很好的例子.
//foo.js, multiple methods
var circle = require('./circle.js');
console.log( 'The area of a circle of radius 4 is ' + circle.area(4));
//circle.js
var PI = Math.PI;
exports.area = function (r) {
return PI * r * r;
};
exports.circumference = function (r) {
return 2 * PI * r;
};
//bar.js
var square = require('./square.js');
var mySquare = square(2);
console.log('The area of my square is ' + mySquare.area());
//square.js, single method
module.exports = function(width) {
return {
area: function() {
return width * width;
}
};
}
Run Code Online (Sandbox Code Playgroud)
我最喜欢的模式是
(function (controller) {
controller.init = function (app) {
app.get("/", function (req, res) {
res.render("index", {});
});
};
})(module.exports);
Run Code Online (Sandbox Code Playgroud)
var mod = require('./mod.js');
Run Code Online (Sandbox Code Playgroud)
require是一个函数,它接受一个名为path的参数,在这种情况下,路径是 ./mod.js
调用require时,会发生一系列任务:
Module.prototype.require在lib/module.js中声明的调用函数,它断言路径存在并且是一个字符串
call Module._load是lib/module.js中一个解析文件的函数Module._resolveFilename(request, parent, isMain),
Module._resolveFilename函数被调用并检查模块是天然的(天然模块通过返回NativeModule在定义的函数LIB /内部/ bootstrap_node.js),如果是的,它会返回模块否则它检查parh的字符(必须在2数./通过lib/internal/bootstrap_node.jsModule._resolveLookupPaths中定义的函数定义的一些字符(路径必须以此开头)var module = new Module(filename, parent);NativeModule.prototype.compile定义的函数进行编译NativeModule.wrap中定义的lib /内部/ bootstrap_node.js需要编译的JavaScript内容mod.js并把它封装:它把它封装在其他一些代码,使所有这些工作.因此,您编写的代码mod.js包含在函数表达式中.这意味着您在节点中编写的所有内容都在V8中运行