7

我通過一個數組傳遞一個jQuery對象到一個函數從另一個文件類似如下:模仿與Visual Studio的JavaScript智能感知鑄造

$(document).bind("loadStoreDisplayCallGoals", function(source, urlParams) 
{ 
    var selectedStoreDocument = urlParams["storeDocument"]; 
} 

selectedStoreDocument應該是一個jQuery對象,但Visual Studio的智能感知永遠認識到這一點。我嘗試添加與$.extend擴展selectedStoreDocument

// cast selectedStoreDocument to a jQuery type 
$.extend(selectedStoreDocument, $); 

然而,延長selectedStoreDocument打掉了我所有的jQuery方法(.each.find,等等)。

我怎樣才能讓selectedStoreDocument在IntelliSense中顯示爲jQuery對象?請注意,我在Visual Studio 2010中工作。

回答

6

我創建了實用功能的實用功能+ VSDoc一個單獨的文件,第二個文件。

utilities.js:

function castToJQuery(item) 
{ 
    return item; 
} 

公用事業-vsdoc.js:

function castToJQuery(item) 
{ 
    /// <summary> 
    ///  1: $(item) - "Casts" the specified item to a jQuery object for the sake of Intellisense 
    /// </summary> 
    /// <returns type="jQuery" /> 
    return $("dummy"); 
} 

現在我可以打電話castToJQuery在我的任何下游的文件,使Visual Studio中認爲動態屬性是一個jQuery目的。

var selectedStoreDocument = castToJQuery(urlParams["storeDocument"]); 
selectedStoreDocument.find("products"); 

Visual Studio現在爲我的動態urlParams [「storeDocument」]使用Intellisense。

+0

有趣的方法。你能發佈它的外觀截圖嗎? – Mrchief

+0

今天晚些時候我會嘗試添加一些屏幕截圖 –

2

您無法獲得動態添加屬性的智能感知。您需要靜態定義它們(在vsdoc或js文件):

$.selectedStoreDocument = function() { 
    ///<summary>A Selected Store Document</summary> 
}; 
+0

所以沒有辦法「中投」的東西智能感知? –

+0

Javascript不是編譯語言,VS也沒有可以評估語言併爲您提供像智能感知等c#語言的服務提供者。 – Mrchief

+0

@Pedar,你應該試試ReSharper 6,它的JavaScript引擎比股票VS設備更適合intellisense(和重構等)的目的。 –

1

你可以像這樣的變量指定文檔的信息:

$(document).bind("loadStoreDisplayCallGoals", function(source, urlParams) 
{ 
    /// <var type="jQuery"/> 
    var selectedStoreDocument = urlParams["storeDocument"]; 
    selectedStoreDocument._ 
} 

欲瞭解更多信息,請參閱http://msdn.microsoft.com/EN-US/library/hh542722(VS.110).aspx

+0

是不是隻適用於VS2012? – IvanH