1.匿名函數概述
關于匿名函數的第一次認識還是在jquery源碼里,打開jQuery首先看到的是
復制代碼 代碼如下:
(function( window, undefined ) {.......................})(window);
復制代碼 代碼如下:
(function( window, undefined ) {
// Define a local copy of jQuery
var jQuery = function( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init( selector, context );
},
.........
window.jQuery = window.$ = jQuery;
})(window);
復制代碼 代碼如下:
;(function ($) {
$.fn.tabing = function (arg) {
instance = new Plugin(this, arg);
};
var instance = null;
function Plugin(element){
this._tabs = $(element);
this._tabli = $("a[href*='#']",this._tabs);
this._tabDiv = this._tabs.siblings().filter("div[id*='tab']");
this.init();
}
Plugin.prototype = {
init: function(){
this._tabli.addClass("unselected");
this._tabli.eq(0).addClass("selected");
this._tabDiv.css("display","none");
this._tabDiv.eq(0).css("display","block");
this._tabli.each(function(){
$(this).bind("click",function(){
for(var i = 0;i<instance._tabDiv.length;i++){
instance._tabDiv.eq(i).css("display","none");
}
instance._tabDiv.filter("#"+$(this).attr("href").split('#')[1]).css("display","block");
});
})
}
}
})(jQuery);
復制代碼 代碼如下:
(function(){
function getObjByID(id){
return document.getElementById(id);
}
function __addClass(id,className,classValue){
$(id).style.className=classValue;
}
window.addClass=__addClass;
})();
復制代碼 代碼如下:
function Video() { };
function Movie() { };
var _video = new Video();
_video.size = 3;
_video.toString = function () {
return "video";
};
_video.getName = function () {
return "VideoXXX";
};
var _movie = new Movie();
(function (parent, child) {
for (var ele in parent) {
if (!child[ele]) //在child不包含該屬性或者方法的時候,才會拷貝parent的一份
child[ele] = parent[ele];
}
})(_video, _movie); //匿名函數調用的方式
alert(_movie.size); //3
alert(_movie.toString()); //[object Object]
alert(_movie.getName()); //VideoXXX
新聞熱點
疑難解答
圖片精選