在jquery ready函数中声明函数的最佳实践 [英] Best practices for declaring functions inside jquery ready function

查看:89
本文介绍了在jquery ready函数中声明函数的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有找到一个很好的参考,用于在

I haven't found a good reference for declaring my own functions inside the

jquery.ready(function(){});

我想声明它们,因此它们在就绪闭包的同一范围内。我不想混淆全局js名称空间,所以我不希望它们在ready闭包之外声明,因为它们只针对里面的代码。

I want to declare them so they are inside the same scope of the ready closure. I don't want to clutter the global js namespace so I don't want them declared outside of the ready closure because they will be specific to just the code inside.

那么如何声明这样的函数......我并不是指一个自定义的jquery扩展方法/函数......只是一个常规的'ol函数,它做了一些微不足道的事情,比如:

So how does one declare such a function...and I'm not referring to a custom jquery extension method/function...just a regular 'ol function that does something trivial say like:

function multiple( a, b ){ 
    return a * b; 
}

我想遵循jquery建议和函数声明语法。我可以通过声明一个类似上面的乘法函数来使它工作......但由于某种原因它看起来不正确所以我想我只需要一些指导。

I want to follow the jquery recommendation and function declaration syntax. I can get it to work by just declaring a function like the multiply one above...but it doesn't look correct to me for some reason so I guess I just need some guidance.

推荐答案

我相信你可以在ready()闭包中声明函数,但是在这里你可以更明确地了解本地作用域:

I believe that you would be okay just declaring the function inside the ready() closure, but here you can be a bit more explicit about the local scoping:

jQuery.ready(function() {

     var myFunc = function() {

           // do some stuff here

     };

     myFunc();


});

这篇关于在jquery ready函数中声明函数的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆