使用$(函数等)启动javascript代码
我正在学习主干和todo示例应用程序 我注意到有3种启动文件中代码的方法:使用$(函数等)启动javascript代码,javascript,jquery,backbone.js,Javascript,Jquery,Backbone.js,我正在学习主干和todo示例应用程序 我注意到有3种启动文件中代码的方法: $(function() { // code here }); $(function( $ ) { // code here }); (function() { // code here }()); 我不明白它们之间的区别,也不知道什么时候应该用一个来代替另一个 我还看到一些人用它来启动他们的代码: $(document).ready(function(){ // code here }); 据我所见,
$(function() {
// code here
});
$(function( $ ) {
// code here
});
(function() {
// code here
}());
我不明白它们之间的区别,也不知道什么时候应该用一个来代替另一个
我还看到一些人用它来启动他们的代码:
$(document).ready(function(){
// code here
});
据我所见,这是完整的写作方式,对吗
在更一般的情况下,我是否应该在每个文件中都包含类似的javascript代码
谢谢你的建议
$(document).ready(function(){})
确保您的代码在DOM ready上运行,这样您就可以访问DOM了。您可以在中阅读更多有关这方面的信息$(function(){})
只是#1的别名。这里的任何代码都将等待DOM就绪()$(function($){})
相当于#1和#2,只有在中获得对jQuery的干净引用(请参见下面的注释)。同样,您可以将$
传递到#1中的函数,它也会做同样的事情(创建对jQuery的本地引用)(function(){}())
只是一个函数,用于创建新的闭包注意:要理解上面第3章中的内容,请记住
$
是jQuery
的别名。但是,jQuery并不是唯一使用$
变量的库。因为$
可能会被其他人覆盖,所以您需要确保在您的范围内,$
将始终引用erence jQuery-因此使用了$
参数
最后,它基本上归结为以下两个选项:
头部
,则必须等待文档准备就绪,因此请使用以下方法:
jQuery(function($) {
// Your code goes here.
// Use the $ in peace...
});
(function($) {
// Use the $ in peace...
}(jQuery));
p.S.要更好地理解闭包和范围,请参阅。这两个:
$(function() {
// code here
});
$(document).ready(function(){
// code here
});
它们是直接等价的,它们都是在文档加载后启动jQuery的方法。前者只是后者的一个较短版本
这个:
(function() {
// code here
}());
只不过是一个具有零参数的作用域函数,它立即被零参数调用。我想,首先应该认识到
$=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 () { });
});
将$
放在括号内可确保jQuery$别名(您可以放心,它总是以这种方式表示jQuery)
最后是一个IIFE(即时调用的函数表达式)
-
顶部的$(底部与jQuery匹配)表示
$(美元符号)表示namepsace范围内的jQuery。
这样做是为了确保其他库不会与开发人员使用的库发生冲突
打算/希望将$与一起使用
(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
现在,如果我们在名称空间之外,我们可以看到这些内部/公共方法和变量是如何受到影响的:
// 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());
第三个示例与其他示例不同。另请参见[JavaScript中自声明匿名函数之前的美元符号?](只是为了避免混淆(在某些情况下是愤怒)自动执行匿名函数并不会真正执行它们自己。它们被创造出来了,但确实如此。发布后,我立即看到了关于(又称生活)的小脚注,所以nvm:P有趣的是,我们都链接到了同一篇文章。@rlemon-而我同意Cowboy(本·阿尔曼)即时调用函数表达式是更准确的术语,自动执行匿名函数已成为引用它们的标准方式。我们都链接到同一篇文章,因为这是生命名称的来源。另外,那篇文章值得一读,不管你怎么称呼它们。它有一些关于这些函数表达式的重要信息ons.Ohh我已经读过了,我个人不在乎我们怎么称呼它们,只要人们知道它们是如何工作的以及为什么要使用它们。我只是在做我的尽职调查并将其链接:P但这一切都不是为了:P你已经拥有了:)我喜欢iLife只是因为它更准确,而且SEAF是一个丑陋的首字母缩略词。@rlemon-我不会说SEAF是一个丑陋的首字母缩略词。但说到他们的发音……拜托!没有什么比“不确定”更令人讨厌的了 :P@JosephSilber:您是否意识到使用包含编号引用的编号列表会使阅读变得混乱?您说
2…只是#1的别名。
和3…相当于#2…
这使它看起来像$(document).ready(function(){})
和$(function(){})
和$(函数($){})
都是一样的。如果这些数字中的一些是指OP问题中未编号的例子,那么你的答案肯定会从一点澄清中受益。-或者可能是我很困惑!无论哪种方式,我肯定可以澄清以排除错误的解释
(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
// 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());