Defining functions in javascript inside jQuery

I work on my own site and I am having some problems. I use jQuery along with the prototype, and I have its names, and so in this question it is assumed that you can use $ or jQ as a reference to names for jQuery.

So, I have a bunch of functions, some jQuery and javascript, some simple javascript, only some jQuery. Now, at present, some functions are defined inside the document.ready jQuery function, and some of them are defined outside it, for example:

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() {
}

, , . , . , undefined. . .

, / , , document.ready ? , . ? . ?

.

+3
3

, namespace, ( /). , collision + , 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);
});

( " " ) /, - , getter .

, , (IMO).

, , - "Javascript: The Good Parts" .

+4

(document).ready , , . (document).ready, - , , . .

, , , . , .

0

, , . , script .

0

Source: https://habr.com/ru/post/1762717/


All Articles