【问题标题】:Passing function pointer as argument, node.js传递函数指针作为参数,node.js
【发布时间】:2012-11-28 21:52:13
【问题描述】:

我正在node.js 中编写http 服务器。 Server 对象有几个字段,应根据请求发送给客户端。这就是为什么我需要将status() 传递给router.route() 的原因——因此可以从内部调用它(在解析请求之后)并返回更新变量值。问题是当status() 被调用时,它不会打印字段值,而是对象字面量。

构造函数Server如下:

this.server = net.createServer(connectionHandler);  
    this.resourceMap = resourceMap;
    this.rootFolder = rootFolder;
    this.isStarted = false;
    this.startedDate = undefined;
    this.port = undefined;
    this.numOfCurrentRequests = 0;

function status() {
    return {
        "isStarted" : this.isStarted,
        "startedDate" : this.startedDate,
        "port" : this.port,
        "resourceMap" : this.resourceMap,
    };
}

function connectionHandler(socket) {
    console.log('server connected');
    console.log('CONNECTED: ' + socket.remoteAddress +':'+ socket.remotePort);
    socket.setEncoding('utf8');
    socket.on('data',function(data) {
            this.numOfCurrentRequests += 1;
            router.route(status,data,socket,handle,resourceMap,rootFolder);
            });
}

this.startServer = function(port) {
    this.port = port;
    this.isStarted = true;
    this.startedDate = new Date().toString();
    this.server.listen(port, function() {
            console.log('Server bound');
        });
}
}

当从router.route() 中调用状态时,我得到

function status() {
        return {
            "isStarted" : this.isStarted,
            "startedDate" : this.startedDate,
            "port" : this.port,
            "resourceMap" : this.resourceMap,
        };
    }

我理解函数的方式是变量,因此按值传递。有什么办法可以解决我的问题?

谢谢

【问题讨论】:

  • 我想你只需要改变这一行:router.route(status,data,socket,handle,resourceMap,rootFolder);到 router.route(status(),data,socket,handle,resourceMap,rootFolder);
  • 我试了一下,结果是[object,object]
  • 啊,它正在显示。也许 JSON.stringify(status()) 会让它看起来更具可读性
  • 我试试吧。但是函数不是像我写的那样按值传递吗?
  • 这就是你使用 () 执行函数的原因——当你得到 JavaScript 对象的 [object, object] 时。对其进行字符串化应该使其在 Bowser 中可读。

标签: node.js function pointers arguments


【解决方案1】:

如果我已经清楚地联系到您,您不需要函数指针,但它是结果。所以status应该如下传递:

router.route(status(),data,socket,handle,resourceMap,rootFolder);

最终将传递以下对象:

return {
        "isStarted" : this.isStarted,
        "startedDate" : this.startedDate,
        "port" : this.port,
        "resourceMap" : this.resourceMap,
    }

如果您想显示它们,请在您的回调中使用以下代码

for(var s in status) {
    console.log(s+" : "+status[s]);
}

【讨论】:

    【解决方案2】:

    对于像我这样在 Google 搜索中实际上是指“函数指针”的人,这里有一个答案:

    承认我们在 app.js 文件中需要一个外部文件:

    var _commandLineInterface("./lib/CommandLine")();
    var _httpServer = require("./lib/HttpServer")(_commandLineInterface);
    

    然后,在我们的 HttpServer.js 文件中,承认我们想要使用 _commandLineInterface 对象作为 _httpServer 构造函数的参数传递。我们会做的:

    function HttpServer(_cli){
        console.log(_cli);
    }
    
    module.exports = HttpServer;
    

    BZZZZZZZZT!错误。 _cli 指针似乎未定义。结束了。一切都丢失了。

    好的...这是诀窍:还记得我们的 CommandLine 对象吗?

    function CommandLine(){
        ...
    
        return this;
    }
    
    module.exports = CommandLine;
    

    是的。当您不习惯 nodejs 行为时,这很奇怪。

    你必须对你的对象说它必须在构造完成后返回自己。而对于习惯于前端 Javascript 行为的人来说,这是不正常的。

    因此,当您添加小“返回技巧”时,您将能够从其他对象内部获取指针:

    function HttpServer(_cli){
        console.log(_cli); // show -> {Object}
    }
    

    希望它能帮助像我这样的一些 nodejs 新手。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-08
      • 2021-12-18
      • 2019-08-17
      • 2013-03-16
      • 2015-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多