简体   繁体   English

从jQuery函数内部公开函数

[英]Making Functions Public from Inside jQuery Function

I have a block of JavaScript/jQuery that works fine. 我有一块运行良好的JavaScript / jQuery。

<script type="text/javascript">
    $(function () {
         function doSomething() {
             // Do something amazing here!
         }
        // Many various jQuery handlers and support functions
    });
</script>

But now I'd like my doSomething() function to be callable from another block of script on the same page. 但是现在我希望可以从同一页面上的另一个脚本块中调用doSomething()函数。

I understand I can do that by moving doSomething() outside of the jQuery function ( $(function () {}) ). 我知道我可以通过将doSomething()移到jQuery函数( $(function () {}) )之外来做到这一点。 But then doSomething() wouldn't be able to call the helper functions inside of the jQuery function. 但是,那么doSomething()将无法在jQuery函数内部调用帮助程序函数。

I could move the other functions outside of the jQuery function, but some of them are handlers that need to be initialized, and they share they same helper functions. 我可以将其他函数移到jQuery函数之外,但是其中一些是需要初始化的处理程序,它们共享相同的帮助器函数。

Is there a way to keep all my functions inside my jQuery function, but just make one of them visible outside of it? 有没有一种方法可以将我的所有函数保留在我的jQuery函数中,而只是让其中一个在其外部可见?

And any suggestions on where I could go to read up on this? 关于我可以去哪里阅读的任何建议?

JavaScript has functional scope. JavaScript具有功能范围。 So, the reason you can't call your function if it is nested within 因此,如果函数嵌套在其中,则无法调用它的原因

$(function () { ... });

is because it is only accessible within that function's scope. 因为它只能在该函数的范围内访问。

You can easily move that function definition : 您可以轻松移动该函数定义

function doSomething() { ... }

outside of the $(function(){...}) function and still have access to variables within the $(function(){...}) function's scope by passing the variables as parameters to the function and then having it return any modifications: $(function(){...})函数之外,并且仍然可以通过将变量作为参数传递给函数,然后让它返回,来访问$(function(){...})函数范围内的变量。任何修改:

    $(function () {
         var blah = 'blah';
         var result;
         result = doSomething(blah);
         // Many various jQuery handlers and support functions
     });
     function doSomething(blah) {
         // Do something amazing here!
         return newBlah;
     }
     // Now you can call your doSomething function in the global scope too
     var example = "test";
     var result = doSomething(example);

Well, in fact you should move your logic from this wrapper. 好吧,实际上您应该从此包装器中移出逻辑。

It's intended for initialization of logic that should run after DOM is ready. 它用于初始化应在DOM准备好后运行的逻辑。 You shouldn't make any functions here. 您不应在此处执行任何功能。

Consider following pattern for your code: 考虑以下代码模式:

(function($) {
  // Your logic here
  // You could safely export your functions to
  // global scope from here, if you really need
  var app;

  app = {
    forms: doSomething
  };

  function doSomething() {
    // Do something amazing here!
  }

  window.app = app; 

  $(function() {/* start interact with DOM */});
}(jQuery));

You can do it extending jQuery: 您可以扩展jQuery:

$.extend({
    foo: new function () {
        var _self = this;            

        _self.doSomething = function () {

        };   

        _self.initialize = function () {
            $('#button-x').click(function(){
                _self.doSomething(); //use the function inside
            }); 
        }; 
});

$(function () {
    $.foo.initialize();
    $.foo.doSomething(); //use the function outside
});

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

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