如何将类属性传递给NodeJS中嵌套在class> method>中的.each块? [重复]

时间:2022-02-12 14:27:07

This question already has an answer here:

这个问题在这里已有答案:

I wonder how I can pass a class property into function located in a .each of a method as below. I have found that I could call the property from the newly created instance but it means the class can't be replicated. Is there a object inheritance friendly way of calling the class property from inside a function located in function > .each > method > class?

我想知道如何将类属性传递给位于.each方法中的函数,如下所示。我发现我可以从新创建的实例调用该属性,但这意味着该类无法复制。是否有一个对象继承友好的方式从函数> .each>方法>类中的函数内部调用类属性?

class ok {
  constructor() {
    this.jojo = "first state";
  }

  myMethod() {
    let rp = require('request-promise-native');
    let cheerio = require('cheerio');
    rp("https://tomydna.com")
      .then(response => {

        let $ = cheerio.load(response);

        $('a').each(function() {
          this.jojo = "second state"; // This does not work
          ok1.jojo = "second state from outside"; // This would work but it is not good because it is related to one instance only. If a new Instance is created the method stops working.
          console.log(ok1.jojo); // will replicate as many times it finds url on the page.
        });


      });

  }

}

ok1 = new ok();
ok1.myMethod();

3 个解决方案

#1


2  

You can assign this object into another variable. You can not use class ok this object into .each function because inside .each this represent current immediate object(.each function callback), following example may help you

您可以将此对象分配给另一个变量。你不能将类ok这个对象用到.each函数中,因为里面.each表示当前的立即对象(.each函数回调),下面的例子可以帮到你

class ok {

    constructor() {
        this.jojo = "first state";
    }

    myMethod() {
        let self = this;
        let rp = require('request-promise-native');
        let cheerio = require('cheerio');
        rp("https://tomydna.com")
            .then(response => {

                let $ = cheerio.load(response);

                $('a').each(function () {
                    self.jojo = "second state"; // This will work
                    ok1.jojo = "second state from outside"; // This would work but it is not good because it is related to one instance only. If a new Instance is created the method stops working.
                    console.log(ok1.jojo); // will replicate as many times it finds url on the page.
                });


            });

    }

}

ok1 = new ok();
ok1.myMethod();

#2


0  

In your .each callback, you can use an arrow function. Be sure though if you want to have a reference to the cheerio dom node that your callback is of the form (index, node) => {/* your code */}.

在.each回调中,您可以使用箭头功能。但是,如果你想要一个对cheerio dom节点的引用,你的回调是表单(索引,节点)=> {/ *你的代码* /}。

#3


0  

its just a scope issue. Assign this to a local variable and then use it inside your each. eg:

它只是一个范围问题。将其分配给局部变量,然后在每个变量中使用它。例如:

  myMethod() {
    let that = this; //local variable
    let rp = require('request-promise-native');
    let cheerio = require('cheerio');
    rp("https://tomydna.com")
      .then(response => {

        let $ = cheerio.load(response);

        $('a').each(function() {
          that.jojo = "second state"; // usage
          console.log(that.jojo); 
        });


      });

  }

#1


2  

You can assign this object into another variable. You can not use class ok this object into .each function because inside .each this represent current immediate object(.each function callback), following example may help you

您可以将此对象分配给另一个变量。你不能将类ok这个对象用到.each函数中,因为里面.each表示当前的立即对象(.each函数回调),下面的例子可以帮到你

class ok {

    constructor() {
        this.jojo = "first state";
    }

    myMethod() {
        let self = this;
        let rp = require('request-promise-native');
        let cheerio = require('cheerio');
        rp("https://tomydna.com")
            .then(response => {

                let $ = cheerio.load(response);

                $('a').each(function () {
                    self.jojo = "second state"; // This will work
                    ok1.jojo = "second state from outside"; // This would work but it is not good because it is related to one instance only. If a new Instance is created the method stops working.
                    console.log(ok1.jojo); // will replicate as many times it finds url on the page.
                });


            });

    }

}

ok1 = new ok();
ok1.myMethod();

#2


0  

In your .each callback, you can use an arrow function. Be sure though if you want to have a reference to the cheerio dom node that your callback is of the form (index, node) => {/* your code */}.

在.each回调中,您可以使用箭头功能。但是,如果你想要一个对cheerio dom节点的引用,你的回调是表单(索引,节点)=> {/ *你的代码* /}。

#3


0  

its just a scope issue. Assign this to a local variable and then use it inside your each. eg:

它只是一个范围问题。将其分配给局部变量,然后在每个变量中使用它。例如:

  myMethod() {
    let that = this; //local variable
    let rp = require('request-promise-native');
    let cheerio = require('cheerio');
    rp("https://tomydna.com")
      .then(response => {

        let $ = cheerio.load(response);

        $('a').each(function() {
          that.jojo = "second state"; // usage
          console.log(that.jojo); 
        });


      });

  }