对NodeJS模块开发的怀疑

Doubts about NodeJS Module Development

本文关键字:怀疑 开发 模块 NodeJS      更新时间:2024-03-31

我正试图编写我的第一个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. 我在第一次迭代中是否正确调用了countcount: function (token) {}(tokens)
  2. 如何递归调用count方法?我得到一个"ReferenceError:计数未定义"
  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;
};