2013-04-08 42 views
1

我正在學習一個JavaScript文件,並在其中看到一些方法被包裝在一個jQuery函數中。任何人都可以幫助我如何調用以下方法嗎?我可能知道什麼是優點,或者爲什麼這個方法被封裝在一個函數中?以下是我的示例JavaScript代碼。如何調用這個包裝在jQuery函數中的JavaScript方法

JQuery的/ JavaScript的

$(document).ready(function() { 
    //How to invoke "testMethod" method? 
    $(function() { 
     function testMethod() { 
      alert("this is a test method"); 
     } 
    }); 
}); 
+4

爲什麼你會使用'$(文件).ready'內'$(文件).ready'? '$(function(){});'是'$(document)的簡寫版本。ready' – 2013-04-08 05:55:50

+0

你打算如何調用它? DOM事件?或從另一個功能? – Joseph 2013-04-08 05:57:12

+0

@Travis J感謝您的信息,現在它回答我的問題「爲什麼」 – 2013-04-08 05:57:47

回答

3

正如你已經聲明它,testMethod()是本地功能,僅在聲明它的功能範圍內使用。如果您希望在該範圍之外對其進行調用,則需要對其進行不同的定義,以便在更廣的範圍內可用。這樣做的

一種方法是使它成爲一個全局函數:

$(document).ready(function() { 
    //How to invoke "testMethod" method? 
    $(function() { 
     window.testMethod = function() { 
      alert("this is a test method"); 
     } 
    }); 
}); 

testMethod(); // available globally now 

它也可以連接到一個全局命名空間,也可能在較高的範圍內它也可以解決你的問題來定義。沒有關於你的情況的具體信息,我們不能建議哪一個最好,但是你需要做的主要事情是改變函數的聲明方式,以便在你想調用它的範圍內可用。

P.S.爲什麼你有一個嵌套在另一個文檔就緒函數?這不會提供額外的功能並增加不必要的複雜性此外,如果您希望在全局範圍內使用文檔就緒處理程序,則確實沒有理由在其中定義testMethod()

+0

不要讓它們成爲全球。將它們分配給不同的範圍,但不要使它們成爲全局範圍。 – 2013-04-08 05:59:38

+0

@IngoBürk - 這取決於OP正在嘗試做什麼。如果他們提供更多細節,我們可以提供其他選項,但是他們的代碼顯然只是用於解決問題的一個例子,所以我們不知道他們在做什麼或試圖解決什麼問題。在一些情況下,全局函數也是有用的,就像我在我的答案中也提到的其他構造一樣。 – jfriend00 2013-04-08 06:00:43

+0

(不downvoter)這是做到這一點的一種方式,但絕對不是一個理想的方式。你總是可以將它附加到另一個全局變量中......比如使用jQuery提供的可用功能'$ .fn'。至少可以利用現有的結構。 – 2013-04-08 06:01:19

0

您的通話需要在$(function之內。

這是關於範圍,你需要打破$(function的testMethod。

您是否可以進一步解釋您的需求,以便我們可以提供更好的幫助?

0

成爲準備就緒事件:

$(document).ready(function() { 
    //How to invoke "testMethod" method? 
    var testMethod = function() { 
     alert("this is a test method"); 
    } 

    // V0.1 
    testMethod(); 

    // V0.2 
    $('#some_id').click(testMethod); 
}); 

在其他部分:

$(document).ready(function(){...}); 
//is the same as 
$(function(){...}} 

至於你的問題,在這裏的都是這樣做的潛在途徑:

myObj = {testMethod: null}; 
$(document).ready(function() { 
    //How to invoke "testMethod" method? 
    myObj.testMethod = function() { 
     alert("this is a test method"); 
    } 
}); 

// Something else 
if(myObj.testMethod) myObj.testMethod(); 
1

別的之前:

  • 如果該功能是每個人都使用了一些實用功能,然後把它提供給所有的一些命名空間,就像在這個叫Utility

    //Utility module 
    (function(ns){ 
        //declaring someFunction in the Utility namespace 
        //it's available outside the ready handler, but lives in a namespace 
        ns.someFunction = function(){...} 
    }(this.Utility = this.Utility || {})); 
    
    $(function(){ 
        //here in the ready handler, we use it 
        Utility.someFunction(); 
    }); 
    
  • 如果他們都住在ready處理程序,並希望它將被處理程序中的所有代碼使用,並在處理程序的最外層聲明它,以便所有嵌套的作用域都可以看到它。

    $(function(){ 
        //declare it in the outermost in the ready handler 
        function someFunction(){...} 
    
        //so we can use it even in the deepest nesting 
        function nestedSomeFunction(){ 
        someFunction(); 
        } 
    
        someElement.on('click',function(){ 
        $.get('example.com',function(){ 
         someFunction(); 
        }); 
        }); 
    
        nestedSomeFunction(); 
        someFunction(); 
    
    }); 
    
相關問題