开始JavaScript代码$(功能等(start javascript code with $(f

2019-06-17 18:28发布

我从研究骨干和待办事项例如应用http://todomvc.com/我注意到有3种severals开始在文件中的代码的方法:

$(function() {
 // code here
});

$(function( $ ) {
 // code here
});

(function() {
 // code here
}());

我不明白的差异,当我应该使用一个比其他。

我也看到了用这个来开始他们的代码中的一些人:

$(document).ready(function(){
  // code here
});

从我所看到的,这是写正确的方式充分?

在更普遍的方式,我就应该总是包括我的javascript代码到类似的东西在每个文件?

谢谢你的建议。

Answer 1:

  1. $(document).ready(function(){})确保你的代码的DOM准备运行,使您可以访问DOM。 你可以阅读更多关于这个jQuery的文档 。

  2. $(function(){})只是一个别名#1。 在这里的任何代码将等待DOM准备( 见文档 )。

  3. $(function($){})相当于#1和#2,只有你得到一个干净的参考jQuery的在局部范围内 (见下面的注释)。 同样,可以通过在$给函数在#1,它会做同样的事情(创建一个本地引用jQuery的)。

  4. (function(){}())仅仅是一个自执行匿名函数 ,用于创建新的闭合。

请注意,这些都不是专门针对骨干。 第3是特定于jQuery的,而#4只是香草的JavaScript。


注意:要了解发生了什么事情在#3上面,记住, $是一个别名jQuery 。 然而,jQuery是不是使用的唯一库$变量。 由于$可能会被别人覆盖,要保证你的范围内, $总是引用jQuery的-因此$说法。


最后,它基本上可以归结为以下2个选项:

  1. 如果您的JavaScript是在加载的head ,你必须等待文件准备好,所以用这个:

     jQuery(function($) { // Your code goes here. // Use the $ in peace... }); 
  2. 如果您在您的文档的底部(的结束标记之前-加载你的JavaScript ,你应该正在这样做 ),那么就没有必要等待文件准备(因为DOM已经解析器到达的时间构建了您脚本),和SEAF (AKA IIFE )就足够了:

     (function($) { // Use the $ in peace... }(jQuery)); 

PS闭包和范围有很好的理解,看到JS101:在范围略教训



Answer 2:

我想这是有道理的展开,取出,通过实现$ = jQuery 。 其中下来阅读匿名函数内的命名空间时,下面的目标会更有意义。 但在本质上,你可以使用其中任何。 人们会使用jQuery()代替$()如果他们使用多个库,并希望$由另一个使用。

$(document).ready(function(){
    // Here we have jQuery(document) firing off the ready event
    // which executes once the DOM has been created in 
    // order to ensure that elements you are trying to manipulate exist.
});

​$(function () {
    // Short-hand version of $(document).ready(function () { });
});

上的document.ready()的更多信息

$括号内确保了jQuery的$别名 (可以是安全的它总是意味着jQuery的这种方式)。

$(function ($) { /* code here : $ always means jQuery now */ }); 

最后你有一个IIFE(Immidiately调用的函数表达式) - IIFE解释

(function (myNameSpace, $) {
    // This is an anonymous function - it is ran instantly
    // Usually used for namespaces / etc
    // This creates a scope/wrapper/closure around everything inside of it
}(window.myNameSpace, jQuery));

顶部的$(与它的底部匹配的jQuery)表示该$(美元符号)代表的jQuery的namepsace的范围之内。 这样做是为了确保其他图书馆没有什么开发计划/想与使用的$碰撞。

(function (myNameSpace, $) {
    // Now because of all of this scope/wrapper/closure awesome...
    // you can create -INTERNAL- variables (sort of like Private variables from other langs) 
    // this variable cannot be accessed outside the namespace unless it is returned / exposed

    var internalVariable = '123'; // Internal

    // Even Internal functions!
    function privateFunction () {
        console.log('this is private!');
    }

    // --------------------------------------------------------
    // Public -method- of nameSpace exposing a private variable
    // Notice we're using the myNameSpace object we exposed at the top/bottom

    myNameSpace.nameSpaceMethod = function () {
        privateFunction(); // we can call the private function only inside of the namespace
        return internalVariable; // now we could get this variable
    };
}(window.myNameSpace, jQuery)); // notice these mirror the above arguments in the anon function

匿名函数的详细信息

现在,如果我们的命名空间之外 ,我们可以看到这些内部/ public方法和变量影响:

// This will come up undefined
alert(internalVariable);

// This will trigger a -method- of the myNameSpace namespace - and alert "123"
// Showcasing how we access a Public method - which itself has access to the internal variable
// and exposes it to us!
alert(myNameSpace.nameSpaceMethod());
​


Answer 3:

这两个:

$(function() {
 // code here
});

$(document).ready(function(){
  // code here
});

直接等同,它们都启动一些jQuery的文件已加载时的方式。 前者只是一个短后者的版本。

这个:

(function() {
 // code here
}());

只是零点的参数,它立即调用零个参数范围的功能。



文章来源: start javascript code with $(function, etc