如何从代码中获取变量a的值?

时间:2021-04-10 01:52:18
request(target, function (err, resp, body) {
    $ = cheerio.load(body);
    links = $('a'); 
    $(links).each(function (i, link) {
        if ($(link).text().match('worker')) {
            var a = $(link).attr('href').toString();
        }
    });
});

I want to use the output of variable a in further program. Any pointers?

我想在进一步的程序中使用变量a的输出。有什么指针吗?

3 个解决方案

#1


2  

You have two alternative ways to give the variable a a global scope:

您有两种替代方法可以为变量赋予全局范围:

  1. Declare it outside the request function (*) and just assign it inside the each cycle (**), i.e.:

    在请求函数(*)之外声明它,并在每个循环(**)内分配它,即:

    var a; // (*)
    request(target, function (err, resp, body) {
        $ = cheerio.load(body);
        links = $('a'); 
        $(links).each(function (i, link) {
            if ($(link).text().match('worker')) {
                a = $(link).attr('href').toString(); // (**)
            }
        });
    });
    
  2. Simply remove the variable declaration inside the each cycle (*), i.e.:

    只需删除每个循环(*)内的变量声明,即:

    request(target, function (err, resp, body) {
        $ = cheerio.load(body);
        links = $('a'); 
        $(links).each(function (i, link) {
            if ($(link).text().match('worker')) {
                a = $(link).attr('href').toString(); // (*)
            }
        });
    });
    

    (If you assign a value to a variable that has not been declared, it will automatically become a global variable).

    (如果为尚未声明的变量赋值,它将自动成为全局变量)。

#2


0  

the var declaration is function-scoped. This means it can only be used within the function it's declared in.

var声明是函数作用域。这意味着它只能在它声明的函数中使用。

Explanation:

说明:

// 'a' does not exist
$(links).each(function (i, link) {
    if($(link).text().match('worker')){
        var a=$(link).attr('href').toString();
        // 'a' exists'
    }
    // 'a' exists'
});
// 'a' does not exist

There are certain 'hacks' out there, like not 'declaring' it with var, let or const. Doing it like that automatically makes it 'global', but this ofcourse has it drawbacks regarding memory usage and potential memory leaks (the same can be achieved with putting var outside of the functions).

那里有一些“黑客”,就像没有用var,let或const'声明'它一样。像这样做会自动使它“全局化”,但是这有一个关于内存使用和潜在内存泄漏的缺点(将var放在函数之外也可以实现)。

A better way to use it might be to split the functionality up a bit more and just let one function call another (or with callbacks, but I hate callbacks and they're not the solution to everything).

使用它的更好方法可能是将功能分开一点,然后让一个函数调用另一个函数(或使用回调,但我讨厌回调,它们不是所有内容的解决方案)。

#3


0  

If you do like this:

如果你喜欢这样:

JavaScript

JavaScript的

var savedLinks;
request(target, function (err, resp, body) {
    savedLinks = [];
    $ = cheerio.load(body);
    links = $('a'); 
    $(links).each(function (i, link) {
        if ($(link).text().match('worker')) {
            var a = $(link).attr('href').toString();
            savedLinks.push(a);
        }
    });
});

Then you can iterate through savedLinks.

然后,您可以遍历savedLinks。

Like this:

喜欢这个:

for(var i = 0; i < savedLinks.length; i++) {
   console.log(savedLinks[i]);
}

You have to call the function request at some point for it to be initialized though of course.

当然,您必须在某个时刻调用函数请求以进行初始化。

#1


2  

You have two alternative ways to give the variable a a global scope:

您有两种替代方法可以为变量赋予全局范围:

  1. Declare it outside the request function (*) and just assign it inside the each cycle (**), i.e.:

    在请求函数(*)之外声明它,并在每个循环(**)内分配它,即:

    var a; // (*)
    request(target, function (err, resp, body) {
        $ = cheerio.load(body);
        links = $('a'); 
        $(links).each(function (i, link) {
            if ($(link).text().match('worker')) {
                a = $(link).attr('href').toString(); // (**)
            }
        });
    });
    
  2. Simply remove the variable declaration inside the each cycle (*), i.e.:

    只需删除每个循环(*)内的变量声明,即:

    request(target, function (err, resp, body) {
        $ = cheerio.load(body);
        links = $('a'); 
        $(links).each(function (i, link) {
            if ($(link).text().match('worker')) {
                a = $(link).attr('href').toString(); // (*)
            }
        });
    });
    

    (If you assign a value to a variable that has not been declared, it will automatically become a global variable).

    (如果为尚未声明的变量赋值,它将自动成为全局变量)。

#2


0  

the var declaration is function-scoped. This means it can only be used within the function it's declared in.

var声明是函数作用域。这意味着它只能在它声明的函数中使用。

Explanation:

说明:

// 'a' does not exist
$(links).each(function (i, link) {
    if($(link).text().match('worker')){
        var a=$(link).attr('href').toString();
        // 'a' exists'
    }
    // 'a' exists'
});
// 'a' does not exist

There are certain 'hacks' out there, like not 'declaring' it with var, let or const. Doing it like that automatically makes it 'global', but this ofcourse has it drawbacks regarding memory usage and potential memory leaks (the same can be achieved with putting var outside of the functions).

那里有一些“黑客”,就像没有用var,let或const'声明'它一样。像这样做会自动使它“全局化”,但是这有一个关于内存使用和潜在内存泄漏的缺点(将var放在函数之外也可以实现)。

A better way to use it might be to split the functionality up a bit more and just let one function call another (or with callbacks, but I hate callbacks and they're not the solution to everything).

使用它的更好方法可能是将功能分开一点,然后让一个函数调用另一个函数(或使用回调,但我讨厌回调,它们不是所有内容的解决方案)。

#3


0  

If you do like this:

如果你喜欢这样:

JavaScript

JavaScript的

var savedLinks;
request(target, function (err, resp, body) {
    savedLinks = [];
    $ = cheerio.load(body);
    links = $('a'); 
    $(links).each(function (i, link) {
        if ($(link).text().match('worker')) {
            var a = $(link).attr('href').toString();
            savedLinks.push(a);
        }
    });
});

Then you can iterate through savedLinks.

然后,您可以遍历savedLinks。

Like this:

喜欢这个:

for(var i = 0; i < savedLinks.length; i++) {
   console.log(savedLinks[i]);
}

You have to call the function request at some point for it to be initialized though of course.

当然,您必须在某个时刻调用函数请求以进行初始化。