在模块之间传递值

时间:2015-01-28 14:05:22

标签: javascript module callback return browserify

我正在为数学测验创建一个问题生成器,其中计算的总和用于以下问题。到目前为止,我已经让生成器(大部分)工作,但是我很难将 exportAll.return 的返回值传递给另一个模块。

我仍然相对较新的js,所以试图了解基本面。

这是我写的从 numGen 生成器返回结果的内容(console.logs都能正常工作):

var exportAll = exportAll || {}

exportAll.init = function(difficulty){
    numGen.init(difficulty)   //initiate numGen with random value
}

exportAll.update = function(start){
    numGen.updateNumber(start)   //update numGen with last answer
}

exportAll.return = function(start, partner, array){
    console.log('start number: ' + start) 
    console.log('question: ' + start + ' + '+ partner)
    console.log('correct answer: ' + (start + partner).toString())
    console.log(array)
}


module.exports = exportAll;

我正在尝试将 exportAll.return 中的值(start,partner,array)传递给此模块:

var exportAll = require('../models/number_gen.js');

exportAll.init('easy'); 

module.exports = [

    {
        "question" : "What is ((START)) + ((PARTNER?))",

        "answer" : [
            (((ARRAY ITEM 1)),
            ((ARRAY ITEM 2)),
            ((ARRAY ITEM 3)),
            ((START + PARTNER))
        ],

        "correct" : (((START + PARTNER)))
    }

]

我一直在阅读有关异步回调的内容,并且我无法返回一个值,但我仍然不完全理解这个概念。

感谢您的帮助!

编辑:

这是numGen的代码:

var numGen = {

    init: function(difficulty){

        var initialMax, initialMin, difficulty

        if (difficulty == 'easy'){
            initialMax = 9
            initialMin = 1
        } else if (difficulty == 'medium'){
            initialMax = 99
            initialMin = 10
        } else if (difficulty == 'hard'){
            initialMax = 999
            initialMin = 100
        }

        var start = Math.floor(Math.random() * initialMax - initialMin) + (initialMin + 1)
        this.getlength(start, difficulty)
    },

    updateNumber: function(firstRand){
        var start = firstRand; 
        this.getlength(start)
    },

    getlength: function(start, difficulty){

        var startString = start.toString().length, maxAmount, minAmount

        if (startString == 1){
            maxAmount = 9
            minAmount = 1
        } else if (startString == 2){
            maxAmount = 99
            minAmount = 10
        } else if (startString == 3){
            maxAmount = 999
            minAmount = 100
        } else if (startString == 4){
            maxAmount = 9999
            minAmount = 1000
        } else if (startString == 5){
            maxAmount = 99999
            minAmount = 10000
        } else if (startString == 6){
            maxAmount = 999999
            minAmount = 100000
        } else if (startString == 7){
            maxAmount = 9999999
            minAmount = 1000000
        } else if (startString == 8){
            maxAmount = 99999999
            minAmount = 10000000
        }

        this.getPartner(maxAmount, minAmount, start, difficulty)
    },

    getPartner: function(max, min, start, difficulty){
        var partner = Math.floor(Math.random() * max - min) + (min + 1)
        this.getAnswer(start, partner, difficulty)
    },

    getAnswer: function(start, partner, difficulty){
        var answer = start + partner
        this.splitAnswer(start, partner, answer, difficulty)
    },

    splitAnswer: function(start, partner, answer, difficulty){

        var answerContain = [], 
            answerLength = answer.length

        if (difficulty == 'easy'){
            maxLengthLimit = 3
            minLengthlimit = 1
        } else if (difficulty == 'medium'){
            maxLengthLimit = 33
            minLengthlimit = 11
        } 

        var scramble1 = answer - (answer.toString().length * Math.floor(Math.random() * maxLengthLimit - minLengthlimit))
        var scramble2 = answer + (answer.toString().length + Math.floor(Math.random() * maxLengthLimit - minLengthlimit))
        var scramble3 = answer - (answer.toString().length - Math.floor(Math.random() * maxLengthLimit - minLengthlimit))

        answerContain.push(scramble1, scramble2, scramble3, answer)

        exportAll.return(start, partner, answerContain)

    }

}

1 个答案:

答案 0 :(得分:0)

你的问题是合法的,但代码有点抽象。无论如何,我还是试一试。您说您在使用exportAll.return方法返回值时遇到问题。但是,我没有看到您实际上return值的位置。

exportAll.return = function(start, partner, array){
        console.log('start number: ' + start);
        console.log('question: ' + start + ' + '+ partner);
        console.log('correct answer: ' + (start + partner).toString());
        console.log(array);

        //something like this
        this.start = start;
        this.partner = partner;
        this.array = array;

        return this; 
    }

希望这能让你走上正轨。但是,如果你能进一步澄清一点,我可以帮助更多。