js匿名函數
(function($){})(jquery) == (function($){})(jQuery)
实际上是匿名函数
用于存放开发插件的代码
作用(非常有用):
这种写法的最大好处是形成闭包。在(function($) {…})(jQuery)在内部定义的函数和变量只能在此范围内有效。
形成是否函数函数、私有变量的概念。比如:
- var i=3;
- function init(){
- alert("外层init:"+i);
- }
- (function($) {
- var i=2;
- function init(){
- alert("内层init:"+i);
- }
- init();
- })(jQuery);
- init();
执行结果:
内层init:2
外层init:3
以下是一個實驗的例子
<script src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
//(function ($) {
// alert(1);
// $.a = function (i)
// {
// alert(i.name);
// this.shows = function (j)
// {
// alert(j);
// alert(3);
// abc();
// def();
// }
// abc = function ()
// {
// alert(4);
// }
// def = function ()
// {
// i.callBack(666666);
// }
// }
//})($);
//var i = new $.a({
// name:"sdy",
// callBack: function (no)
// {
// alert(no);
// alert(1234);
// }
//});
//i.shows(123);
function MM($) {
alert(2);
$.a = function (i) {
alert(i.name);
this.shows = function (j) {//this為必須寫的
alert(j);
alert(1);
cc();
}
aa = function () {
alert("aa");
}
bb = function () {
alert("bb");
}
cc = function ()
{
i.callBank("回調函數");//回調函數 callBack 寫錯也沒事 哈哈哈哈
}
}
};
MM(jQuery);
var abc = new $.a({
name: "sdy", callBank: function (no)
{
alert(no);
}
});
abc.shows(1);
//(function ($) {
// alert(1);
// alert($);
//})(jQuery);
//function MM($) {
// alert(2);
//}
//MM(jQuery)
// (function(str){alert(str)})("aaa");
////相当于:
//function OutPutFun(str) { alert(str); };
//OutPutFun("output");
js匿名函數的更多相关文章
- js正則函數 match、exec、test、search、replace、split 使用介紹集合
match 方法 使用正則表達式模式對字元串執行查找,並將包含查找的結果作為數組返回. stringObj.match(rgExp) 參數 stringObj 必選項.對其進行查找的 String 對 ...
- js函數
函數是什麼?函數就是被事件驅動或者調用執行的可重複的代碼塊. 函數聲明: 使用關鍵詞function,關鍵詞function大小敏感. function a{代碼塊} 局部變量: 在函數內部聲明的變量 ...
- oracle decode(nvl(estimate_qty,0),0,1,estimate_qty) 函數
oracle decode(nvl(estimate_qty,0),0,1,estimate_qty) 函數
- JS匿名函数的理解
js匿名函数的代码如下:(function(){ // 这里忽略jQuery 所有实现 })(); 半年前初次接触jQuery 的时候,我也像其他人一样很兴奋地想看看源码是什么样的.然而,在看到源码的 ...
- Math.Round函數
Math.Round這個函數的解釋是將值按指定的小數位數舍入,但並不就是四捨五入.這種舍入有時稱為就近舍入或四舍六入五成雙 其實在 VB, VBScript, C#, J#, T-SQL 中 Roun ...
- JS匿名函数自执行函数
JS匿名函数自执行函数:(function(){})();(function(){}) 这是一个函数,函数后面接(),则是调用函数 比如(function(arg){console.log(arg); ...
- C#判断奇偶数的函數
// 现代流行的"程序员" public static bool IsOdd(int n) { while (true) { switch (n) { : return true; ...
- PHP 計算字符串長度函數
PHP內置的字符串長度函數strlen無法正確處理中文字符串,它得到的只是字符串所占的字節數.對於GB2312的中文編碼,strlen得到的值是漢字個數的2倍,而對於UTF-8編碼的中文,就是3倍的差 ...
- SQL窗体函數一例
需求: MSSQL,列出服務實例中全部數據庫的例如以下信息: 數據庫ID.數據庫名.創建日期.數據文件類型.數據文件大小.數據庫總大小.文件所在路徑. 寫法(後面的百分比為所花時間占比): -- 连接 ...
随机推荐
- 【leetcode❤python】342. Power of Four
#-*- coding: UTF-8 -*- class Solution(object): def isPowerOfFour(self, num): ""& ...
- baidu 快递查询API
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- require或include相对路径多层嵌套引发的问题
require或include相对路径多层嵌套引发的问题 php中require/include 包含相对路径的解决办法 在PHP中require,include一个文件时,大都是用相对路径,是个 ...
- Formatting Excel File Using Ole2 In Oracle Forms
Below is the some useful commands of Ole2 to format excel file in Oracle Forms.-- Change font size a ...
- maquee 无缝轮播
页面的自动滚动效果,可由javascript来实现, 但是有一个html标签 - <marquee></marquee>可以实现多种滚动效果,无需js控制. 使用marquee ...
- 配置Servlet3.0的方式和注意事项!
- CSS布局口诀 - CSS BUG顺口溜
在进行CSS网页布局时遇到BUG,请认真阅读以下内容,非常容易记忆的,不知道哪位高人把CSS BUG编成了顺口溜了!看看好不好记住呢? 一.IE边框若显若无,须注意,定是高度设置已忘记: 二.浮动产生 ...
- Oracle 怎么让id自增加
--自增长序列 create table test( tid int not null, tname ), tsex ), tbz ) ) --添加主键约束 alter table test add ...
- [Effective Java]第二章 创建和销毁对象
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- hdu 1800 (map)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 Flying to the Mars Time Limit: 5000/1000 MS (Java/ ...