2011-11-25 17 views
2

可能性の重複:javascriptとfnで何が起こっていますか?

(function ($, window, undefined) 
{ 

    var 
    // String constants for data names 
    dataFlag = "watermark", 
    dataClass = "watermarkClass", 
    dataFocus = "watermarkFocus", 
    dataFormSubmit = "watermarkSubmit", 
    dataMaxLen = "watermarkMaxLength", 
    dataPassword = "watermarkPassword", 
    dataText = "watermarkText", 

    // Copy of native jQuery regex use to strip return characters from element value 
    rreturn = /\r/g, 

    // Includes only elements with watermark defined 
    selWatermarkDefined = "input:data(" + dataFlag + "),textarea:data(" + dataFlag + ")", 

    // Includes only elements capable of having watermark 
    selWatermarkAble = "input:text,input:password,input[type=search],input:not([type]),textarea", 

    // triggerFns: 
    // Array of function names to look for in the global namespace. 
    // Any such functions found will be hijacked to trigger a call to 
    // hideAll() any time they are called. The default value is the 
    // ASP.NET function that validates the controls on the page 
    // prior to a postback. 
    // 
    // Am I missing other important trigger function(s) to look for? 
    // Please leave me feedback: 
    // http://code.google.com/p/jquery-watermark/issues/list 
    triggerFns = [ 
     "Page_ClientValidate" 
    ], 

    // Holds a value of true if a watermark was displayed since the last 
    // hideAll() was executed. Avoids repeatedly calling hideAll(). 
    pageDirty = false, 

    // Detects if the browser can handle native placeholders 
    hasNativePlaceholder = ("placeholder" in document.createElement("input")); 


    /*******************************************************/ 
    /* Enable/disable other control on trigger condition */ 
    /*******************************************************/ 
    $.fn.TriggerContol = function (options) 
    { 

イム最後の2行に苦しん:
What does jQuery.fn mean?

私はそのやっは何をうまくしようとしているページ上のいくつかのスクリプトを持っています。なぜ開発者はfnを使用しましたか?これはどういう意味ですか?

このファイル全体は基本的に、jQuery(x).TriggerControlを実行できるように、関数をJqueryライブラリにアタッチするために設計された匿名関数を呼び出すことを理解しています。 Imは、この特定の構成が何を意味するのだろうと思っています。

答えて

2

jQueryでは、jQuery.fnは、単にjQuery.prototypeへの参照です。

何が起こっているのかを理解するには、プロトタイプの継承を理解する必要があります。

jQueryコンストラクタから作成されたすべてのオブジェクトは、jQuery.prototypeオブジェクトに存在するすべてのプロパティを継承します。

この簡単な例ください:これは明らかに非常に簡単で、jQueryのはちょうどこのよりもはるかに多くを行いますが、それはすべてのjQueryオブジェクトはの同じセットを持つことができます原型継承ある

function jQuery() { 
     // empty constructor function 
} 

jQuery.fn = jQuery.prototype; // Make a "fn" property that simply references the 
           // jQuery.prototype object. 

// So assigning a property to jQuery.fn 
// is the same as assigning it to jQuery.prototype 
jQuery.fn.sayHello = function() { alert("Hi!"); }; 

// Create an object from the jQuery constructor 
var obj = new jQuery; 

// Our new object will automatically inherit the "sayHello" function 
obj.sayHello(); // "Hi!" 

をメソッド。

すべてのjQueryオブジェクトに新しいメソッドを追加するには、そのプロトタイプを追加するだけです。

jQuery.fn.sayGoodbye = function() { alert("Goodbye!"); }; 

// call the new method on our original object 
obj.sayGoodbye(); // Goodbye! 

あなたは私たちが直接私たちのobjにそのプロパティを追加していないにもかかわらず、sayGoodbyeが正しく魔法登場し、見ることができるように。上記コードの


JSFIDDLE DEMO


私はそれがより短く、そしておそらく継承がどのように動作するか原型知らない誰かのために理解しやすいですので、彼らはプロパティとしてjQuery.fnを提供する理由は単純であることを前提としています。

+1

+1すてきな答え... – ManseUK

1

fn演算子を使用すると、(セットの)要素に対して独自のメソッドを作成できます。メソッドがTriggerControlと呼ばれ、同じように呼び出すことができ、この場合:

$('.someclass').TriggerControl(someoptions); 

詳細はthisページをご覧ください。

関連する問題