在 jQuery 中定义 Javascript 函数

发布于 2024-09-17 11:45:38 字数 847 浏览 4 评论 0原文

我正在一个专有网站上工作,但遇到了一些问题。我将 jQuery 与原型一起使用,并且我已经对其进行了正确的命名空间,因此在这个问题中假设您可以使用 $ 或 jQ 作为对 jQuery 的命名空间引用。

所以我有很多函数,有些混合了 jQuery 和 javascript,有些是纯 javascript,有些只是 jQuery。现在,目前有些函数是在 document.ready jQuery 函数内定义的,有些是在其外部定义的,有点像这样:

jQ(document.ready(function($) {

  if ( ifConfig ) {
    //page check, function calls here
    fnc1();
    fnc2();
    fnc3();
    fnc4();
  }

  function fnc1() {
    //fnc code in here
  }
  function fnc2() {
    //fnc code in here
  }
});  //end document.ready

function fnc3() {
}
function fnc4() {
}

现在这都是伪代码,您可以假设这些函数是有效的并且其中包含有效的代码。最近我正在做一些调试,我在 document.ready 中声明和调用的函数之一说它是未定义的。我将其移到 document.ready 之外,一切又恢复正常了。

我基本上是想更好地理解函数如何启动/调用的顺序,所以我的问题是何时在 document.ready 内部声明函数以及何时在外部声明它们?您是否仅在该 document.ready 中调用它们时才在内部声明?或者我应该总是在 document.ready 之外声明它们?

谢谢。

I'm working on a proprietary site, and I'm having some issues. I'm using jQuery along with prototype, and I've got it namespaced properly, so in this question assume you can use $ or jQ as a namespaced reference to jQuery.

So I've got a bunch of functions, some mix jQuery and javascript, some plain javascript, some jQuery only. Now, currently some functions are defined within the document.ready jQuery function, and some are defined outside of it, kind of like this:

jQ(document.ready(function($) {

  if ( ifConfig ) {
    //page check, function calls here
    fnc1();
    fnc2();
    fnc3();
    fnc4();
  }

  function fnc1() {
    //fnc code in here
  }
  function fnc2() {
    //fnc code in here
  }
});  //end document.ready

function fnc3() {
}
function fnc4() {
}

Now this is all pseudo code, you can assume the functions are valid and have valid code in them. Recently I was doing some debugging, and one of my functions that was declared and called inside the document.ready said it was undefined. I moved it outside of the document.ready, and everything worked again.

I'm basically trying to understand the order of how functions are initiated/called better, so my question is when do you declare functions inside the document.ready and when do you declare them outside? Do you only declare inside when they're called within that document.ready only? Or should I always just declare them outside of that document.ready?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉风有信 2024-09-24 11:45:38

一般来说,您应该声明 &定义您自己的命名空间,所有应用程序逻辑(包括函数/方法)都位于其中。这样您就可以避免与站点上的其他脚本发生冲突,这样您的代码就会更加干净且更易于维护。

var myapp = function(){
    var foobar1 = null,
        foobar2 = null,
        foobar3 = null;

    return {
        getFoobar1:  function(){
           return foobar1;
        },
        getFoobar2:  function(){
           return foobar2;
        },
        setFoobar1:  function(foo){
           foobar1 = foo;
        },
        clickhandler: function(e){
           alert('I am an event handler, and I am not anonymous');
        }
        // etc.
    };
};

$(document).ready(function(){
    var Application = myapp();

    Application.getFoobar2();

    $(document).bind('click', Application.clickhandler);
});

该模式(有些人称之为“方法模式”)创建一个封闭的函数/对象,它还保证命名空间内的私有成员变量,只能通过外部的 getter 函数访问。

这实际上只是一个非常基本的例子,你可以推动这个想法&图案延伸,这是非常好的&一件好事(IMO)。

Douglas Crockford 的《Javascript:The Good Parts》是一本关于这方面内容的好书,经常被命名和推荐。

Generally, you should declare & define your own namespace, where all of your application logic (including functions/methods) is located. That way you avoid collision with other scripts on your site + that way your code is much cleaner and easier to maintenaine.

var myapp = function(){
    var foobar1 = null,
        foobar2 = null,
        foobar3 = null;

    return {
        getFoobar1:  function(){
           return foobar1;
        },
        getFoobar2:  function(){
           return foobar2;
        },
        setFoobar1:  function(foo){
           foobar1 = foo;
        },
        clickhandler: function(e){
           alert('I am an event handler, and I am not anonymous');
        }
        // etc.
    };
};

$(document).ready(function(){
    var Application = myapp();

    Application.getFoobar2();

    $(document).bind('click', Application.clickhandler);
});

That pattern (some call it the "method pattern") creates a closured function/object which also guarantees private member variables within your namespace, only accessible through the getter functions from the outside.

This is really only a pretty basic example, you can push this idea & pattern to an extend, which is very nice & a good thing (IMO).

A great book about this stuff which was named and recommended pretty often is "Javascript: The Good Parts" by Douglas Crockford.

荒芜了季节 2024-09-24 11:45:38

如果函数仅在文档就绪函数内部使用,则在内部声明它,这样就不会污染全局范围。否则,请在外部声明它,以便脚本的其余部分可以访问这些函数。

If a function is only used inside the document ready function, then declare it inside so you don't pollute the global scope. Otherwise, declare it outside so it the rest of your script has access to those functions.

汐鸠 2024-09-24 11:45:38

(document).ready 更多地用于需要在页面加载时执行的事情,而不是函数声明。如果您在 (document).ready 内部声明它们,它们的作用域将是该块的本地范围 - 如果它们仅在本地使用,那很好,它们应该在那里声明。否则,将其声明到外部。

因此,在您的示例中,如果函数仅在该块中使用,则应在其中声明它们。如果另外在其他地方使用,则应对外声明。

(document).ready is more used for things that need to be executed at page load, and not function declarations. If you declare them inside of (document).ready, their scope will be local to that block - if they're only used locally, that's fine and they should be declared there. Otherwise, declare them outside.

So in your example, if the functions are only used in that block, they should be declared in there. If they're used other places additionally, they should be declared outside.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文