node.js文件中的单独函数

时间:2017-04-30 09:19:09

标签: javascript node.js

我想在node.js文件中分隔函数。我不知道该怎么做。

以下是一个例子:

----template----
<script>
//for time
        $('#date-input').on('click', '#state', function(){
            $('#my_form input').on('change',function(){
                console.log($('input[name=date]:checked', '#form_date').val());
                $.ajax({
                    url: "{% url 'booktime' %}",
                    type: 'post',
                    data: {},
                    success: function(data){
                        alert(data);    
                    },
                    failure: function(){
                        alert("Contact admin server error");
                    }
                });
                });
            });
</script>

我想导出window.onload = function () { document.getElementsByTagName("button").addEventListener("click", function () { alert("hi"); var bgcolor = "#" + Math.floor(Math.random() * 16777215).toString(16); document.body.style.background = bgcolor; }); } &amp; var var1 = 'I want to use this var along across all included files'; var var2 = 'I want to use this var along across all included files'; var actions = { send1() { //function 1 with var1 & var2 }, send1a() { //function 1a with var1 & var2}, send2() { //function 2 with var1 & var2}, send2a() { //function 2a with var1 & var2} }; send1()中的send1a()&amp; function1.js中的send2(),以便拥有干净的文件并使用send2a()&amp; function2.js

我该如何正确地做到这一点?

2 个答案:

答案 0 :(得分:2)

创建2个模块文件: fun1.js

var var1 = "CONSTANT"


var actions = {
    // Whatever you want to export, you can write it here
    send1: function () {
        console.log("send1", var1)
    },
    send1a: function () {
        console.log("send1a")
    },
    a: "asdsad",
    b: 45,
    c: [1, 2, 3, 4, 5, 6],
    var1: var1
}


module.exports = actions

/*
 Or you can export using
 module.exports = {
     actions: actions,
     variableX: "something"
 }

 then in other modules, import it as
 var fun = require('./module_path')

 And use it as fun.variableX, fun.actions.send1 etc
 */

fun2.js

// Import variable from other module
var fun1 = require('./fun1')
// or just import var1
// var var1 = require('./fun1').var1

var actions = {
    send2: function () {
        console.log("send2", fun1.var1)
    },
    send2a: function () {
        console.log("send2a")
    }
}


module.exports = actions

然后在主文件中

main.js

// Import all modules
var fun1 = require('./fun1')
var fun2 = require('./fun2')

console.log(fun1.a) // Prints array

// fun1 will have all exported modules from fun1.js file
fun1.send1() // Prints send1 CONSTANT
fun1.send1a()

// fun2 will have all exported modules from fun2.js file
fun2.send2()  // Prints send2 CONSTANT
fun2.send2a()

答案 1 :(得分:1)

你想要做的是将一个包含所有功能的对象与其“名称”一起作为键 -
你实际上并不需要用引号括起来的键 - 为了清楚起见,我在这里做了。

var functions1 = require('./function1')
var functions2 = require('./function2')

var actions = {
    'send1':  functions1.send1
    'send1a': functions2.send1a
    ...
};

module.exports.actions = actions;

要通过actions对象调用函数,您需要做的就是这样 -

var actionToDo = 'send1'; // key value that holds the function.
actions[actionToDo]() // here is the actual call to the `send1()` function from the `functions1,js` module.