2011-01-29 73 views
2

我剛開始學習jQuery和OO Javascript,所以我試圖弄清楚哪些屬於哪個以及他們做了什麼。我jQuery的文檔跨此代碼來需要幫助解密JQuery示例

(function($sub) { 

    $sub // a subclass of jQuery 
    $sub === jQuery; //= false 

    $sub.fn.myCustomMethod = function(){ 
    return 'just for me'; 
    } 

    $sub(document).ready(function() { 
    $sub('body').myCustomMethod(); //= 'just for me' 
    }); 

})(jQuery.subclass()); 

問題:

  1. 爲什麼(函數所包圍()
  2. 這是什麼意思(jQuery.subclass());是?這是一個JQuery的事情或常規的JavaScript的一部分嗎?

感謝

回答

5

(1)自調用函數

function() { 

}(); 

都對自己無效的,因爲它們會導致語法錯誤,它的混亂看到它調用的底部。

這就是爲什麼JavaScript社區在自調用函數啓動時使用(讓其他代碼讀者意識到它不是一個正常函數。

var o = (function() { 

})(); 

每當我看到有(啓動功能,我知道,這是自調用。這意味着它立即執行並且o包含函數的返回值而不是函數。

(2)的jQuery子類

警惕,使用子類依賴於具有jQuery的1.5。我建議你等待官方發佈,而不是使用1.5 RC1,因爲有一些問題需要解決。

jQuery.subclass是jQuery 1.5 beta的一部分。這是jQuery的一個新功能,它實質上構成了jQuery的一個子類。

這意味着您添加到jQuery.subclass的任何內容都不會添加到jQuery中。這個jQuery子類有jQuery的所有功能,但任何您添加或更改不會影響「全球jQuery的」

註釋樣品來源

// self invoking function. This creates a closure so that anything declared in 
// this function is local and doesn't effect global state 
(function($sub) { 

    $sub // a subclass of jQuery 
    $sub === jQuery; //= false 

    // here we extend $.fn with a new method. This actually extends the prototype of $sub 
    // $sub.fn === $sub.prototype === $() 
    // $sub.fn is actually a new jQuery object. This means that when using $sub we first look 
    // for method defined on $sub.fn, and if there aren't any we look on methods defined 
    // on $.fn. A useful feature of this is that you can overwrite methods 
    $sub.fn.myCustomMethod = function(){ 
    return 'just for me'; 
    } 

    // We can "overwrite" methods of jQuery by hiding the original methods. 
    $sub.fn.attr = function() { 
    alert("new attr!"); 
    // you can go into $sub.superclass to get the original $ object and call methods there 
    $sub.superclass.attr.apply(this, arguments); 

    } 

    // This calls .ready from the original jQuery 
    $sub(document).ready(function() { 
    $sub('body').myCustomMethod(); //= 'just for me' 
    }); 

// we pass in a subclass of jquery at the bottom. This means that $sub has all the 
// functionality of $ but doesn't change $ itself. 
})(jQuery.subclass()); 

免責聲明.__proto__棄用。它僅用於說明jQuery.subclass原型鏈的外觀。

對於那些誰瞭解.__proto__(此獲取對象的構造函數的原型)

var $sub = jQuery.subclass(); 
$sub.__proto__ === $sub.fn; // true 
$sub.__proto__.__proto__ === $.fn; // true 

// example of .__proto__ 
function constructor() { } 
var o = new constructor; 
o.__proto__ === constructor.prototype; // true 

如果需要進一步的解釋,或者如果您想了解什麼都請不要客氣。我可能會掩飾一些我認爲很明顯的東西。

2
  1. 這是一個返回匿名函數的表達式。使用jQuery.subclass())將結果函數立即調用(第二對圓括號)作爲單個參數。
  2. 這是一個jQuery的東西。它允許你創建一個「自定義」 JQuery的類,同時仍留有可供其他腳本不變JQuery的頁面上(http://api.jquery.com/jQuery.subclass/
+0

@Goleztroll technolally該表達式返回調用後自外部括號結束後調用的函數的值。 – Raynos 2011-01-29 23:29:05

+0

不,它不。只有函數聲明包含在括號中。括號在函數的關閉`}`之後關閉。 – GolezTrol 2011-01-29 23:33:21

0

我會盡力解釋它

  1. 功能被包圍(),因爲這是你如何製作javascript closure。全局範圍(窗口)內的()代碼將不可見。代碼使用一個參數創建匿名函數,並且在使用作爲函數參數傳遞的jQuery.subclass()進行聲明後調用該函數。

例如:

function dirty($arg) {...}是從全球範圍

(function dirty($arg) {...})("some arg");不是在全局範圍內可見可見 - 它創建了它自己的範圍內的功能,立即調用它,如果它不分配到一個變量,引用丟失

以這種方式,你可以改變jQuery的功能而不用改變全局jQuery對象。我不知道這怎麼可能是有用的,但是這就是它:) 2.從jQuery的API文檔 -

創建了jQuery 對象的子類,允許您添加額外的 或改變jQuery方法和 屬性,而不影響全局jQuery的 。