Simple javascript question

I have a jquery file with 2000 lines, I just split the file into smaller ones. If I have a function in the first file, this file # 2 refers to it, it approaches undefined.

Each file is wrapped in a jquery ready function, what is the best way to do this?

+3
source share
7 answers

A function definition should not be wrapped in another function. Not if you really want this function definition to be private. And if I understand correctly that this is not your intention.

Only calling the jQuery ready function assurance function.

If you are worried that your functions are faced with third-party names, then they will forgive them in space:

var myFunctions = {}
myFunctions.doThis = function () {}
myFunctions.doThat = function () {}

, mashup . , javascript.

+2

​​ ready, , ready.

, :

function foo()
{
    alert('foo');
}

$(document).ready(function()
{
    foo();
});

P.S. ready :

$(function()
{
   foo();
});

: ready , , . , , , ready, .

: , :

$(function()
{
    var foo = 'foo';
    var bar = 'bar';

    alert(foo);
    alert(bar);
});

:

$(function()
{
    var foo = 'foo';
    var bar = 'bar';
});

$(function()
{
    alert(foo);
    alert(bar);
});

, foo bar , , .

, , ( ).

+6

, , ; ... , . document.ready, script , document.ready, .

+1

, , , undefined , , .

. jQuery, $.extend. , , .

. . , .

+1

. , .

0

JavaScript. jquery.ready , . JavaScript , a la

$(document).ready(function ()
{
   functionFromFile1();
   functionFromFile2();
};
0

"wrapped in a jquery ready function" is nothing more than a material binding to an event readythat fires when jQuery thinks the DOM is ready.

You should only bind methods that are DOM dependent to the event ready. No matter how many bindings you make, all methods will be executed in the binding order at the end.

0
source

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


All Articles