Node.js引用错误:未定义类名

Node.js reference error : Classname is not defined

本文关键字:未定义 错误 js 引用 Node      更新时间:2023-09-26

我有一个文件bloom.js,如下所示:

     function Bloom(k, m, n, hashFunction){
        if(!m)
            m = 1000
        this.m = m;
        if(!n)
            n = 100
        this.n = n;
        if(!k)
            k = Math.max(Math.round(m / n * Math.LN2), 1)
        this.k = k

        this.insert = function(string){
            for(var i = 0; i < this.k; i++){
                var index = parseInt(this.hashFunction(i + string), 16) % this.array.length
                this.array[index] = 1;
            }
            return true;
        }


    }
module.exports = Bloom;

在我的main.js中,我在执行以下操作时出错:

var Bloom=require("./Bloom");var bloom=new bloom()

错误:

TypeError: Bloom is not a function
at Object.<anonymous> (J:'code'Main.js:114:13)
at Module._compile (module.js:409:26)

如何解决此错误?我也试过导出模块,但没有成功。

// bloom.js
   function Bloom(k, m, n, hashFunction){
    if(!m)
        m = 1000
    this.m = m;
    if(!n)
        n = 100
    this.n = n;
    if(!k)
        k = Math.max(Math.round(m / n * Math.LN2), 1)
    this.k = k

    this.insert = function(string){
        for(var i = 0; i < this.k; i++){
            var index = parseInt(this.hashFunction(i + string), 16) % this.array.length
            this.array[index] = 1;
        }
        return true;
    }
}
module.exports = Bloom;

然后,在main.js:中

// NOTE! the variable name here is what matters, not what you defined in bloom.js
var Bloom = require("./bloom");
var bloom = new Bloom();