簡體   English   中英

關於NodeJS模塊開發的疑問

[英]Doubts about NodeJS Module Development

我正在嘗試編寫我的第一個NodeJS模塊,但是我很難掌握一些概念。

這是我目前擁有的代碼( genexer計數器/生成器):

"use strict";

var ret = require('ret');

module.exports = function (regex) {
    if (Object.prototype.toString.call(regex) === '[object RegExp]') {
        regex = regex.source;
    }

    else if (typeof regex !== 'string') {
        regex = String(regex);
    }

    try {
        var tokens = ret(regex);
    }

    catch (exception) {
        return false;
    }

    return {
        charset: '',
        reference: [],
        count: function (token) {
            var result = 0;

            if ((token.type === ret.types.ROOT) || (token.type === ret.types.GROUP)) {
                if (token.hasOwnProperty('stack') === true) {
                    result = 1;

                    token.stack.forEach(function (node) {
                        result *= count(node);
                    });
                }

                else if (token.hasOwnProperty('options') === true) {
                    var options = [];

                    token.options.forEach(function (stack, i) {
                        options[i] = 1;

                        stack.forEach(function (node) {
                            options[i] *= count(node);
                        });
                    });

                    options.forEach(function (option) {
                        result += option;
                    });
                }

                if ((token.type === ret.types.GROUP) && (token.remember === true)) {
                    reference.push(token);
                }
            }

            else if (token.type === ret.types.POSITION) {
            }

            else if (token.type === ret.types.SET) {
                token.set.forEach(function (node) {
                    if (token.not === true) {
                        if ((node.type === ret.types.CHAR) && (node.value === 10)) {
                        }
                    }

                    result += count(node);
                });
            }

            else if (token.type === ret.types.RANGE) {
                result = (token.to - token.from + 1);
            }

            else if (token.type === ret.types.REPETITION) {
                if (isFinite(token.max) !== true) {
                    return Infinity;
                }

                token.value = count(token.value);

                for (var i = token.min; i <= token.max; ++i) {
                    result += Math.pow(token.value, i);
                }
            }

            else if (token.type === ret.types.REFERENCE) {
                if (reference.hasOwnProperty(token.value - 1) === true) {
                    result = 1;
                }
            }

            else if (token.type === ret.types.CHAR) {
                result = 1;
            }

            return result;
        }(tokens),
        generate: function () {
            return false;
        },
    };
};

問題:

  1. 我在第一次迭代時是否正確調用了count count: function (token) {}(tokens)
  2. 我如何遞歸調用count方法? 我得到一個“ReferenceError:count not defined”
  3. 這是使用多種方法定義小模塊的正確(或最佳實踐)方法嗎?

請原諒我沒有發表3個不同的問題,但我對所有術語還不是很熟悉。

  1. 立即調用閉包的約定是count: (function(args) {return function() {}})(args)但你的方式也適用於所有環境。
  2. 你不能因為計數是一個關閉不幸 - 見3。
  3. 如果要在模塊內部使用模塊上的方法,我會在return語句之外聲明模塊。 如果你想要一個很好的例子,請參閱下划線 / lodash源代碼。

因此,您可以使用類似下面骨架的聲明來定義模塊

module.exports = function (regex) {
    //...
    var count = function(tokens) {
        //...
        return function() {
           //...
           var ret *= count(node);


           return ret;
        }
    }
    var mymod = {
        count: count(tokens)
        //...
    };
    //...
    return mymod;

};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM