简体   繁体   English

如何在全局中设置javascript函数?

[英]How can I set javascript function in global?

I want to set my function in global js. 我想在全局js中设置函数。 So it can called from anywhere 所以它可以从任何地方调用

I have function this : 我有这个功能:

$(function () {
    function formatCurrency(input) {
        return input.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1.");
    }
});

I put the function on the 我把功能放在

C:\\xampp\\htdocs\\mysystem\\resources\\assets\\js\\main.js C:\\ XAMPP \\ htdocs中\\ mysystem \\资源\\资产\\ JS \\ main.js

And I add this : <script src="{{URL::asset('/resources/assets/js/main.js')}}"></script> on the 然后在此添加<script src="{{URL::asset('/resources/assets/js/main.js')}}"></script>

C:\\xampp\\htdocs\\mysystem\\resources\\views\\layouts\\app.blade C:\\ XAMPP \\ htdocs中\\ mysystem \\资源\\意见\\布局\\ app.blade

When executed, on the console exist error like this : 当执行时,在控制台上存在如下错误:

GET http://mysystem.dev/resources/assets/js/main.js 404 (Not Found) GET http://mysystem.dev/resources/assets/js/main.js 404(未找到)

How can I solve it? 我该如何解决?

You should define you function as a method of the window object. 您应该将函数定义为窗口对象的方法。

window.formatCurrency = function(input) {
    return input.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1.");
}

Another more clean way it would be to define your function as a method of an object called Utils , like below: 另一种更干净的方法是将函数定义为称为Utils的对象的方法,如下所示:

var Utils = (function(){
    function formatCurrency(input){
        return input.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1.");
    }
    return {
        formatCurrency: formatCurrency
    }
}());

Doing so, you will not pollute the global namespace. 这样做不会污染全局名称空间。 You will have only one variable defined in the global namespace called Util, which would contain an obejct with useful functions that can be used by other parts of your application. 您将在全局名称空间中仅定义一个称为Util的变量,该变量将包含一个对象,该对象包含可以由应用程序的其他部分使用的有用功能。 As it is you can't see the benefits of the latter approach. 因为它是您看不到后一种方法的好处。 However you can consider how it would be if you wanted to use 3 more functions, that by their own use 2 other functions as helpers. 但是,您可以考虑如果要再使用3个函数,而自己又使用2个其他函数作为助手,情况会如何。 If that was the case your return statement would also contained the 3 more functions, whereas the 2 functions I mentioned before wouldn't be epxosed at all ! 如果是这样的话,您的return语句还将包含另外3个函数,而我之前提到的2个函数根本不会被使用!

The issue seems to be you're using asset() wrong. 问题似乎是您使用的asset()错误。

asset refers to a file in /public . 资产是指/public的文件。

You should compile/minimize your JS for production and put it somewhere in /public , ie /public/js/my.js 您应该编译/最小化用于生产的JS,并将其放在/public某个位置,即/public/js/my.js

You can define global function like this 您可以这样定义全局函数

window.formatCurrency = function(input){
            return input.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1.");
        }

and you are define function in ready function($(function)) then you must call this in ready function also. 并且您在ready函数($(function))中定义了函数,那么您也必须在ready函数中调用它。 So better define it outside of ready function or in self invoking function. 因此,最好在就绪功能之外或在自调用功能中定义它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM