使用另一个js文件中的函数

时间:2017-03-15 18:57:34

标签: javascript

嗨,我正在写一个使用js的机器人,我试图使用几个文件,但我不能使用其他js文件的功能,有没有办法,我可以使用几个文件? 谢谢你的帮助 只是一些例子:

fl.js:

bla = function(x){
  return 100*x;
}

index.js:

var num=prompt('Enter number');
console.alert(dd.bla(num));

1 个答案:

答案 0 :(得分:0)

使用以下模式:

在module.js文件中:

(function ($, module) {

    module.add = function (a, b) {
        return a+b;
    };

    module.subtract = function (a, b) {
        return a-b;
    };
}(jQuery, window.module = window.module || {}));

模块的用法如下:

(function ($, module) {

    $(function () {

        $("#add").on("click", function(){
            var first = $("#first").val();
            var second = $("#second").val();

            var result = module.add(first, second);

            alert(result);
        });
    });

}(jQuery, module));

plnkr test