【问题标题】:Mimicking Casting with Visual Studio's JavaScript IntelliSense使用 Visual Studio 的 JavaScript IntelliSense 模拟投射
【发布时间】:2011-07-26 14:56:09
【问题描述】:

我通过如下数组将一个 jQuery 对象从另一个文件传递到一个函数中:

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

selectedStoreDocument 应该是一个 jQuery 对象,但是 Visual Studio Intellisense 永远不会这样识别它。我尝试使用$.extend 添加扩展selectedStoreDocument

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

但是,扩展 selectedStoreDocument 消除了我所有的 jQuery 方法(.each.find 等)。

如何让selectedStoreDocument 在 IntelliSense 中显示为 jQuery 对象?请注意,我正在使用 Visual Studio 2010。

【问题讨论】:

    标签: javascript visual-studio visual-studio-2010 intellisense


    【解决方案1】:

    我为实用函数创建了一个单独的文件,并为实用函数 + 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 现在可以与 Intellisense 一起用于我的动态 urlParams["storeDocument"]。

    【讨论】:

    • 有趣的方法。你能发一张它的截图吗?
    • 今天晚些时候我会尝试添加一些截图
    【解决方案2】:

    您无法为动态添加的属性获取智能感知。您需要静态定义它们(在 vsdoc 或 js 文件中):

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

    【讨论】:

    • 所以没有办法为智能感知“投射”一些东西?
    • Javascript 不是编译语言,VS 也没有可以评估语言并为您提供类似智能感知的 c# 的语言服务提供商。
    • @Pedar,你应该试试 ReSharper 6,它的 javascript 引擎比用于智能感知(和重构等)的库存 VS 工具要强大得多
    • 不,我明白。我只是不确定是否有可用于类似于 ///A Selected Store Document 的变量的注释标签,我可以用它来“转换”我的变量。
    • 不确定您所说的演员阵容是什么意思。以下是对可用标签的引用:msdn.microsoft.com/en-us/library/bb385682.aspx。你在找param type吗?
    【解决方案3】:

    您可以像这样为变量指定文档信息:

    $(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

    【讨论】:

      猜你喜欢
      • 2014-05-20
      • 1970-01-01
      • 2016-10-31
      • 1970-01-01
      • 2016-07-14
      • 1970-01-01
      • 2012-10-23
      • 2013-02-09
      • 2017-01-08
      相关资源
      最近更新 更多