如何在nodejs中创建另一个javascript类的对象
您好,我想在另一个如何在nodejs中创建另一个javascript类的对象,javascript,node.js,Javascript,Node.js,您好,我想在另一个index.js文件中创建ProfileComparator的对象,但我遇到了一个错误 strategy.js 我能够在strategy.js中创建ProfileComparator的对象,而不是像下面这样的其他javascript文件 var cosineAlgo = new ProfileComparator(cosineAlgoStrategy, "x", "y", 0.9); return cosineAlgo.findMatches(); index.js 我试图在
index.js
文件中创建ProfileComparator
的对象,但我遇到了一个错误
strategy.js
我能够在strategy.js
中创建ProfileComparator
的对象,而不是像下面这样的其他javascript文件
var cosineAlgo = new ProfileComparator(cosineAlgoStrategy, "x", "y", 0.9);
return cosineAlgo.findMatches();
index.js
我试图在index.js中执行同样的操作,但我在这里遇到了一个错误:
var strategyUtils = require("./strategy");
function computeSimilarity(x, user) {
var cosineAlgo = new ProfileComparator(cosineAlgoStrategy, x, y, 0.9);
return cosineAlgo.findMatches();
}
堆栈跟踪:
ReferenceError: ProfileComparator is not defined
at computeSimilarity (/user_code/index.js:187:24)
at /user_code/index.js:232:16
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
有人知道如何解决吗?Fix
您需要导出ProfileComparator
添加:module.exports=ProfileComparator
到strategy.js文件的末尾
现在,您可以在其他文件中使用类似以下内容要求它:
var ProfileComparator=require('path/to/the/module')代码>
别忘了签出:
错误的解释
发生错误的原因是您试图创建ProfileComparator的新实例,但该实例不在范围内 修理
您需要导出ProfileComparator
添加:module.exports=ProfileComparator
到strategy.js文件的末尾
现在,您可以在其他文件中使用类似以下内容要求它:
var ProfileComparator=require('path/to/the/module')代码>
别忘了签出:
错误的解释
发生错误的原因是您试图创建ProfileComparator的新实例,但该实例不在范围内 您需要像这样从strategy.js
文件导出ProfileComparator
comparator函数
module.exports = ProfileComparator
var ProfileComparator = require("./strategy");
在index.js中,需要这样的索引
module.exports = ProfileComparator
var ProfileComparator = require("./strategy");
您需要像这样从strategy.js
文件导出ProfileComparator
comparator函数
module.exports = ProfileComparator
var ProfileComparator = require("./strategy");
在index.js中,需要这样的索引
module.exports = ProfileComparator
var ProfileComparator = require("./strategy");
改变
var ProfileComparator = function(algo, x, y, threshold) {
this.algo = algo;
this.x = x;
this.y = y;
this.threshold = threshold;
};
到
导入为
Import { ProfileComparator } from '<file location>'
从“”导入{ProfileComparator}
更改
var ProfileComparator = function(algo, x, y, threshold) {
this.algo = algo;
this.x = x;
this.y = y;
this.threshold = threshold;
};
到
导入为
Import { ProfileComparator } from '<file location>'
从“”导入{ProfileComparator}
这里是导出的类和算法,您可能想在比较器中使用它们:
const ProfileComparator = function(algo, x, y, threshold) {
this.algo = algo
this.x = x
this.y = y
this.threshold = threshold
}
ProfileComparator.prototype.findMatches = function() {
return this.algo(this.x, this.y, this.threshold)
}
const cosineAlgoStrategy = function(x, y, threshold) {
var similarityCount = cosineUtils.cosineSimilarity(x, y)
if (similarityCount >= threshold) {
return y
}
console.log('------------------------------------')
console.log('cosine')
console.log('------------------------------------')
}
const pearsonAlgoStrategy = function(x, y, threshold) {
console.log('------------------------------------')
console.log(threshold)
console.log('------------------------------------')
}
module.exports = { ProfileComparator, cosineAlgoStrategy, pearsonAlgoStrategy }
这将是新的index.js文件,您可以在其中导入所有需要的模块:
const { ProfileComparator, cosineAlgoStrategy, pearsonAlgoStrategy } = require("./strategy");
function computeSimilarity(x, user) {
var cosineAlgo = new ProfileComparator(cosineAlgoStrategy, x, y, 0.9);
return cosineAlgo.findMatches();
}
这里是您的导出类和算法,您可能希望在比较器中使用它们:
const ProfileComparator = function(algo, x, y, threshold) {
this.algo = algo
this.x = x
this.y = y
this.threshold = threshold
}
ProfileComparator.prototype.findMatches = function() {
return this.algo(this.x, this.y, this.threshold)
}
const cosineAlgoStrategy = function(x, y, threshold) {
var similarityCount = cosineUtils.cosineSimilarity(x, y)
if (similarityCount >= threshold) {
return y
}
console.log('------------------------------------')
console.log('cosine')
console.log('------------------------------------')
}
const pearsonAlgoStrategy = function(x, y, threshold) {
console.log('------------------------------------')
console.log(threshold)
console.log('------------------------------------')
}
module.exports = { ProfileComparator, cosineAlgoStrategy, pearsonAlgoStrategy }
这将是新的index.js文件,您可以在其中导入所有需要的模块:
const { ProfileComparator, cosineAlgoStrategy, pearsonAlgoStrategy } = require("./strategy");
function computeSimilarity(x, user) {
var cosineAlgo = new ProfileComparator(cosineAlgoStrategy, x, y, 0.9);
return cosineAlgo.findMatches();
}
我没有看到任何导出您没有从strategy.js
文件导出ProfileComparator
。请在函数名之前使用“导出”。然后可以在本地文件之外访问它。我看不到任何导出您没有从strategy.js
文件导出ProfileComparator
。请在函数名之前使用“导出”。然后它将在本地文件之外访问,而不是在nodejs中本机工作,因为export/import
尚未标准化。在nodejs中本机工作,因为export/import
尚未标准化。我现在得到这个ReferenceError:cosineAlgoStrategy未在computeSimilarity定义(/user_code/index.js:187:42)at/user_code/index.js:232:16 at process._tickDomainCallback(internal/process/next_tick.js:135:7)
herevar cosineAlgo=new ProfileComparator(cosineAlgoStrategy,x,y,0.9);
我现在得到这个引用错误:cosinealgostrategrategy在computesimility没有定义(/user_code/index.js:187:42)at/user_code/index.js:232:16 at process._tickDomainCallback(internal/process/next_tick.js:135:7)
herevar cosineAlgo=new ProfileComparator(cosineAlgoStrategy,x,y,0.9);
我现在得到这个引用错误:cosinealgostrategrategy在computesimility没有定义(/user\u code/index.js:187:42)at/user\u code/index.js:232:16 at process.\u tickDomainCallback(internal/process/next\u tick.js:135:7)
herevar cosineAlgo=new ProfileComparator(cosineAlgoStrategy,x,y,0.9)
对,这个变量不在范围内!我稍后会将解决方案添加到我的答案中!我现在得到这个ReferenceError:cosinalgostrategy没有在computeSimilarity(/user\u code/index.js:187:42)和/user\u code/index.js:232:16的进程中定义。_tickDomainCallback(internal/process/next\u tick.js:135:7)
herevar cosineAlgo=new ProfileComparator(cosineAlgoStrategy,x,y,0.9);
Right,这个变量不在范围内!我稍后会在我的答案中添加解决方案!我得到TypeError:ProfileComparator不是computeSimilarity的构造函数(/user\u code/index.js:191:20)在/user\u code/index.js:236:16 at process.\u tickDomainCallback(internal/process/next\u tick.js:135:7)
@Williams我更正了我的代码。我只是在键入我的想法。非常感谢:)它成功了。我不知道我们可以用这个导出多个syntax@Williams不客气。当然你可以使用这个“技巧”,创建一个导出对象,然后在导入端销毁它以更轻松地访问功能。如果我在这里添加var而不是constconst{ProfileComparator,cosineAlgoStrategy,pearsonAlgoStrategy}=require(“./strategy”);
那么它就不能工作了吗?我得到了TypeError:ProfileComparator在进程的/user\u code/index.js:191:20处不是computeSimilarity(/user\u code/index.js:236:16)处的构造函数
@Williams我更正了我的代码。我只是在输入我的想法。非常感谢:)它成功了。我不知道我们可以用这个导出多个syntax@Williams不客气。当然你可以使用这个“技巧”,创建一个导出对象,然后在导入端销毁它以更轻松地访问功能。如果我在这里添加var而不是constconst{ProfileComparator,cosineAlgoStrategy,pearsonAlgoStrategy}=require(“./strategy”);
那么它就不能工作了?