简体   繁体   English

如何将模块模式 function 导出到另一个 JavaScript 文件

[英]How can I export module pattern function to another JavaScript file

I have two JavaScript files and would like to export the file with module pattern to another file.我有两个 JavaScript 文件,想将具有模块模式的文件导出到另一个文件。 The file with module pattern, I would like to export only public members.带有模块模式的文件,我只想导出公共成员。 When I try to execute the test, it says that "Calculator is not a constructor".当我尝试执行测试时,它说“计算器不是构造函数”。

file: calculator.js文件:calculator.js

var Calculator = function(){
    var total = null;

    return {      
        add: function(x,y){
            total = x + y; 
        },

        getTotal: function(){
            return total;
        };

        display: function(){
            console.log(total);
        }
    }
}

second file: testCalculator.js第二个文件:testCalculator.js

const calculatorObj = require('calculator.js');

describe("Calculator test suite", function(){
    var calculatorObj = new Calculator();

    it('Verify sum method', function() {        
        try{
            calculatorObj.add(5,5);                

            //assertive
            expect(10, calculatorObj.getTotal());
        }
        catch(err)
        {
            alert(err);
        }
    });
});

It is not working, because you need to export something before consuming it.它不起作用,因为您需要在使用之前导出一些东西。

Add this code to make it work添加此代码以使其工作

module.exports = function(){
    var total = null;

    return {      
        add: function(x,y){
            total = x + y; 
        },

        getTotal: function(){
            return total;
        };

        display: function(){
            console.log(total);
        }
    }
}

do I have to list all the function names that I want to export?我必须列出所有要导出的 function 名称吗?

Yes, you have to export all things you want to import later.是的,你必须导出所有你想稍后导入的东西。 Or you can export one single object with all code you need.或者您可以导出一个 object 以及您需要的所有代码。

do i need to use "import" or "require()"我需要使用“import”还是“require()”

For Node.js, require is common practice.对于 Node.js, require是常见的做法。 Latest Node.JS supports import construction, but, at least for now, using require is preferable.最新的 Node.JS 支持import构造,但至少目前,使用 require 更可取。

For browsers, you will use import notation.对于浏览器,您将使用import符号。

And your function is not a classical constructor, please, take a look into the class notation而您的 function 不是经典构造函数,请查看class符号

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在javascript中从另一个文件调用模块导出函数 - How to call module export function from another file in javascript 如何将 javascript function 导出为模块 - how to export javascript function as a module JavaScript模块模式-如何在使用对象/函数之前使构造函数/初始化函数启动? - JavaScript Module Pattern - How can I get a constructor/init function to fire before the object/function is used? Javascript 将 function 中的 function 导出到另一个文件中的 ZC1C425268E68385D1AB54074F14Z - Javascript Export a function in a function to a function in another file 如何将 JavaScript 对象的所有功能导出为模块,而无需对文件进行大量更改? - How can I export all functions of a JavaScript object as module without having to make a lot of changes to the file? 如何从 javascript 内置的 class 模块中导出 function? - How do I export a function from a module for a class that is built into javascript? 我正在尝试导出一个函数以导入到另一个 javascript 页面,但无法弄清楚如何使用导出/导入来做到这一点 - i am trying to export a function to import into another javascript page but can not figure out how to do it with exports/imports 如何使用另一个Javascript文件中的函数? - How can I use a function from one Javascript file in another? 我可以使用带有javascript模块模式的类吗? - Can I use classes with the javascript module pattern? JavaScript的模块模式不是功能吗? - Module pattern javascript not a function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM