先看下例子html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="jquery-1.10.2_d88366fd.js"></script> </head> <body> <script> $(function(){ function console1(){ console.log('js1:console1'); } }) </script> <script> $(function(){ console1(); }) </script> </body> </html>
这样写 console1函数没法执行报错,说没找到console1函数。jquery
想了半天,原来每一个$(function(){})都是一个函数,函数都有本身的做用域,匿名函数至关于私有的函数,要把匿名函数改为 全局函数就能够在下面用了。函数
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="jquery-1.10.2_d88366fd.js"></script> </head> <body> <script> var console1; $(function(){ console1=function (){ console.log('js1:console1'); } }) </script> <script> $(function(){ console1(); }) </script> </body> </html>