0

我正在使用 JavaScript 类编写一个用于工作的小工具。我不喜欢回调嵌套和嵌套,所以我喜欢将它们分解成单独的函数。我正在尝试从 for...of 循环中运行请求,该请求需要回调。问题是,当我将回调分解为一个单独的函数时,它会失去 for...in 循环的范围,并且它不知道“service.url”是什么。

这是我的代码:

const DATA = require("../resources/data.json");
const Request = require("request");

class Net {
    constructor(){}

    _handleResponses(e, r, b) {
        console.log("--------------------------------------------------------------");

        if(r.statusCode == 404) {
            console.log("\x1b[31m"+`[RESPONSE](Status  ${r.statusCode})`, "\x1b[0m");
            console.log("\x1b[31m"+`Server: ${service.server}`, "\x1b[0m");
            console.log("\x1b[31m"+`Service: ${service.service}`, "\x1b[0m");
            console.log("\x1b[31m"+`Body: ${b}`, "\x1b[0m");
        } else {
            console.log(`[RESPONSE](Status  ${r.statusCode})`);
            console.log(`Server: ${service.server}`);
            console.log(`Service: ${service.service}`);
            console.log(`Body: ${b}`);
        }
    }

    pollServices() {
        for(let service of DATA) {
            Request(service.url, this._handleResponses);
        }
    }
}

module.exports = Net;

这是“pollServices”函数/方法运行时出现的错误:

C:\Users\payton.juneau\Desktop\Me\Projects\Node\com.etouchmenu.tools.crystalBall\utilities\net.js:17 console.log( Server: ${service.server}); ^

ReferenceError:服务未在 Request._handleResponses [as _callback] 中定义(C:\Users\payton.juneau\Desktop\Me\Projects\Node\com.etouchmenu.tools.crystalBall\utilities\net.js:17:36)在 Request.self.callback (C:\Users\payton.juneau\Desktop\Me\Projects\Node\com.etouchmenu.tools.crystalBall\node_modules\request\request.js:186:22) 在 Request.emit (事件.js:159:13) 在请求中。(C:\Users\payton.juneau\Desktop\Me\Projects\Node\com.etouchmenu.tools.crystalBall\node_modules\request\request.js:1163:10) 在 Request.emit (events.js:159:13 ) 在传入消息。(C:\Users\payton.juneau\Desktop\Me\Projects\Node\com.etouchmenu.tools.crystalBall\node_modules\request\request.js:1085:12) 在 Object.onceWrapper (events.js:254:19 ) 在 incomingMessage.emit (events.js:164:20) 在 endReadableNT (_stream_readable.js:

任何帮助将不胜感激,对所有这类东西来说都是新的..

4

1 回答 1

1

当您在一个类上定义 2 个方法时,它们每个都会创建自己的独立范围:

class Example {
  method1() {
    // scope of method 1 only visible to method 1
    let hiddenInM1 = 'example'; // <-- only available here
  }
  method2() {
    // scope of method 2 only visible to method 2
    // I don't know what m1 is
  }
}

这些方法有 3 种方法可以相互共享值。

1.使用这两种方法都属于的外部作用域中可用的变量:

let sharedGlobal = 'example';
class Example {
  method1() {
    // I can see sharedGlobal
  }
  method2() {
    // I also can see sharedGlobal
  }
}

这通常是不明智的,因为全局状态容易出现错误

2.通过这些方法所属的类的上下文(通过this

class Example {
  constructor() {
    this.sharedClassVar = 'example'
  }
  method1() {
    // I can see this.sharedClassVar
  }
  method2() {
    // I also can see this.sharedClassVar
  }
}

3.通过相互传递论据。

class Example {
  method1(fromMethod2) {
    // I can receive stuff from method2
  }
  method2() {
    this.method1('example')
  }
}

如果您查看您的代码,这些模式都不存在,因此_handleResponses无法访问service定义的 in pollServices

你可以做的最简单的改变是通过service你自己:

_handleResponses(service, e, r, b) {
  //             ^^^^^^^ receive the service here
}

pollServices() {
  for (let service of DATA) {
    Request(service.url, (...args) => this._handleResponses(service, ...args))
    //                                                      ^^^^^^^ pass the service with the args
  }
}
于 2018-01-25T22:22:50.580 回答