简体   繁体   English

casper js我如何将varibales从一个homade函数传递给另一个

[英]casper js how can i pass varibales from one homade function to another

okay so i have started building my own modules to use in casperjs i have come to a point where i want to pass variable from one function to another here is what i have below 好的,所以我已经开始构建自己的模块以在casperjs中使用,我已经到了要在其中将变量从一个函数传递给另一个函数的地步,这是我下面的内容

my modules -- functions 我的模块-功能

exports.accdata = function(accnum, amnum) {
    var accountnumber = casper.fetchText('div.arabic:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(' + accnum + ') > td:nth-child(2) > a:nth-child(1)');
    var amountwithtype =casper.fetchText('div.arabic:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(' + amnum + ') > td:nth-child(4) > div:nth-child(1)');
    var redir = accountnumber.substr(1);
    var split = amountwithtype.split (' '); 
    var amount = split[0];
    var type = split[1];
};

exports.job = function (in1, in2){
    console.log(in1);
    console.log(in2);
};

and then how i am trying to pass is my casperjs script 然后我想通过的是我的casperjs脚本

casper.then(function(){
   universe.accdata("3", "3");
   universe.job(amount, type);
});

i am very new and really need help on how to set functions to pass data from on and another 我是一个非常新的人,确实需要有关如何设置函数来传递数据的帮助。

Your accdata function is just setting a bunch of variables then not using them. 您的accdata函数只是设置一堆变量,然后不使用它们。 The simplest solution is to put both functions into one but another solution might be to return an object from your first function and pass it as a parameter to the second function. 最简单的解决方案是将两个函数放在一起,但另一个解决方案可能是从第一个函数返回一个对象并将其作为参数传递给第二个函数。

exports.accdata = function(accnum, amnum) {
    return {
        accountnumber: casper.fetchText('div.arabic:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(' + accnum + ') > td:nth-child(2) > a:nth-child(1)'),
        amountWithType: casper.fetchText('div.arabic:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(' + amnum + ') > td:nth-child(4) > div:nth-child(1)'),
        ...
    };
};

exports.job = function(data) {
    console.log(data);
};

Then use it like this: 然后像这样使用它:

casper.then(function() {
    universe.job(universe.accdata("3", "3"));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM