简体   繁体   English

使用Visual Studio的JavaScript IntelliSense模仿Casting

[英]Mimicking Casting with Visual Studio's JavaScript IntelliSense

I am passing in a jQuery object into a function from another file via an array like the following: 我通过如下所示的数组将jQuery对象传递到另一个文件的函数中:

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

selectedStoreDocument should be a jQuery object, however Visual Studio Intellisense will never recognize it as such. selectedStoreDocument应该是一个jQuery对象,但Visual Studio Intellisense永远不会识别它。 I tried adding extending selectedStoreDocument with $.extend : 我尝试使用$.extend添加扩展的selectedStoreDocument

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

However, extending selectedStoreDocument wiped out all of my jQuery methods ( .each , .find , etc.). 但是,扩展selectedStoreDocument清除我的所有jQuery方法( .each.find等)。

How can I get selectedStoreDocument to appear as a jQuery object in IntelliSense? 如何让selectedStoreDocument在IntelliSense中显示为jQuery对象? Note that I am working in Visual Studio 2010. 请注意,我在Visual Studio 2010中工作。

I created a separate file for utility functions, and a second file for the utility functions + VSDoc. 我为实用程序函数创建了一个单独的文件,为实用程序函数+ VSDoc创建了第二个文件。

utilities.js: utilities.js:

function castToJQuery(item)
{
    return item;
}

utilities-vsdoc.js: 公用事业,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");
}

Now I can call castToJQuery in any of my downstream files to make Visual Studio think a dynamic property is a jQuery object. 现在我可以在任何下游文件中调用castToJQuery,以使Visual Studio认为动态属性是一个jQuery对象。

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

Visual Studio now works with Intellisense for my dynamic urlParams["storeDocument"]. Visual Studio现在可以与Intellisense一起使用我的动态urlParams [“storeDocument”]。

You cannot get intellisense for dynamically added properties. 您无法获得动态添加属性的智能感知。 You need to define them statically (in a vsdoc or js file): 您需要静态定义它们(在vsdoc或js文件中):

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

You can specify documentation information for a variable like this: 您可以为此变量指定文档信息:

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

For more information see http://msdn.microsoft.com/EN-US/library/hh542722(VS.110).aspx 有关更多信息,请参阅http://msdn.microsoft.com/EN-US/library/hh542722(VS.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM