gpt4 book ai didi

javascript - Node.js 模块插件

转载 作者:行者123 更新时间:2023-11-30 18:15:46 25 4
gpt4 key购买 nike

我希望找出编写 Node.js 模块的最佳实践是什么,特别是代码分离要遵循的 javascript 模式类型。

我一直在使用的一种风格是这样的:

var Something;

Something = (function() {

function Something() {
}

Something.prototype.some = function() {

}

return Something;

})();

module.exports = Something;

另一种风格是:

module.exports = {
item: "one",
some: function() {

},
another: function() {

}
}

在 node.js 中,是否有任何理由不建议使用第二种方法?或者是否有其他首选格式,优势是什么?

谢谢!

最佳答案

您在使用“this”关键字时遇到了问题。您必须使用它而不是“某物”。所以 Something.some() 你可以运行……但是如果你想在底部运行它,你必须运行 this.run()。 this 的范围在其他函数中发生变化,可能会变得困惑。

关于javascript - Node.js 模块插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13292947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com