Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 我可以让nodejs插件替换“吗?”;“出口”;对象_Node.js - Fatal编程技术网

Node.js 我可以让nodejs插件替换“吗?”;“出口”;对象

Node.js 我可以让nodejs插件替换“吗?”;“出口”;对象,node.js,Node.js,当nodejs插件是上述类型时,我可以将其用作 function myfunc() { // some codes... } exports = myfunc; < p>可以通过直接设置: MyFunc < /C>作为导出< /Cord>对象: var myfunc = require("./myfunc"); myfunc(); 本文档介绍了导出和模块之间的区别。导出: 请注意,exports是对模块的引用。exports使其适合 仅用于增强。如果要导出单个项目,例如 构造函数

当nodejs插件是上述类型时,我可以将其用作

function myfunc() {
    // some codes...
}

exports = myfunc;

<如何在C++中实现这个类型?< /p> < p>可以通过直接设置:<代码> MyFunc < /C>作为<代码>导出< /Cord>对象:

var myfunc = require("./myfunc");
myfunc();
本文档介绍了
导出
模块之间的区别。导出

请注意,
exports
是对
模块的引用。exports
使其适合 仅用于增强。如果要导出单个项目,例如 构造函数,您将希望使用
模块。直接导出

function myfunc() {
}

module.exports = myfunc;

关于使其成为一个简单的例子,一个粗略的例子是:

function MyConstructor (opts) {
  //...
}

// BROKEN: Does not modify exports
exports = MyConstructor;

// exports the constructor properly
module.exports = MyConstructor;

谢谢你的回答。还有一个问题。节点的init()函数只接受导出参数。如何从Init()函数获取模块句柄?@chaeyk要访问
module
,请定义
Init
以获取第二个
句柄。上面的代码片段演示了这一点。而且,需要明确的是,
Init
是插件源代码中的一个函数,而不是节点中的函数。它可以被命名为任何名称。只需指示节点通过将其传递到
Node\u MODULE()
来调用它。。NODE_MODULE()宏将modname和register函数设置为NODE_MODULE_struct struct,register_func的定义如下。。结构节点模块结构{int version;void*dso\u句柄;const char*文件名;void(*register\u func)(v8::handle target);const char*modname;};因此,无法接受Init(导出,模块)
#include <node.h>

using namespace v8;

Handle<Value> MyFunc(const Arguments& args) {
  HandleScope scope;
  return scope.Close(Undefined());
}

void Init(Handle<Object> exports, Handle<Object> module) {
    module->Set(
        String::NewSymbol("exports"),
        FunctionTemplate::New(MyFunc)->GetFunction()
    );
}

NODE_MODULE(target_name, Init);
node-gyp rebuild