代码之家  ›  专栏  ›  技术社区  ›  Serguei Fedorov

获取用于外部JavaScript文件(.js)的代码提示

  •  2
  • Serguei Fedorov  · 技术社区  · 14 年前

    我想知道如何将jquery导入.js文件。我希望能够使用代码提示(intelligence),但由于这是一个单独的文件,我无法做到这一点。也许我不必进口,但有什么办法吗

    $().[hint]
    

    显示在一个独立的.js文件中?

    2 回复  |  直到 14 年前
        1
  •  7
  •   Charlie    14 年前

    把这个放在js文件的开头

    /// <reference path="/js/jquery.js" />
    
        2
  •  0
  •   anthony sottile    14 年前

    以下是微软关于javascript的intellisense页面: http://msdn.microsoft.com/en-us/library/bb385682.aspx

    我已经使用了他们的格式,而且总体效果很好。jQuery有一个导出(jQuery-[version]-vsdoc.js或类似的东西),您可以将它放在Scripts目录中。

    然后添加 ///<reference path="path/to/jquery-[version].js" /> 在你的脚本顶部,一切似乎都很正常(对大多数事情来说)。

    关于模式的文档非常有限,但我已经创建了一个关于模式外观的粗略XSD(尽管VS并没有获得他们声称支持的所有东西)

    编辑:如果您安装VS2010的SP1,您应该获得对JS intellisense的最新支持。如果没有,微软有一个扩展来支持它。要获得上面提到的-vsdoc.js文件,最简单的方法是通过nuget获得它

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <!-- Schema is based on http://weblogs.asp.net/bleroy/archive/2007/04/23/the-format-for-javascript-doc-comments.aspx -->
      <!-- para tags are not specced on that page but are included as part of the VS JScript Editor Extensions -->
      <!-- The "End" tag is a wrapper tag so I can make a valid document -->
      <xs:element name="End">
        <xs:complexType>
          <xs:choice>
            <xs:element name="reference" minOccurs="0" maxOccurs="unbounded" type="referenceType"/>
            <xs:sequence>
              <xs:element name="summary" type="TextContent"/>
              <xs:element name="param" minOccurs="0" maxOccurs="unbounded" type="ParamNamedTypedTextContent" />
              <xs:element name="field" minOccurs="0" maxOccurs="unbounded" type="NamedTypedTextContent" />
              <xs:element name="returns" minOccurs="0" maxOccurs="1" type="TypedTextContent" />
            </xs:sequence>
          </xs:choice>
        </xs:complexType>
      </xs:element>
    
      <xs:complexType name="referenceType">
        <xs:attribute name="path" type="xs:string" use="required" />
        <xs:attribute name="assembly" type="xs:string" use="optional" />
        <xs:attribute name="name" type="xs:string" use="optional" />
      </xs:complexType>
    
      <xs:complexType name="TextContent" mixed="true">
        <xs:sequence>
          <xs:element name="para" minOccurs="0" maxOccurs="unbounded" type="xs:string" />
        </xs:sequence>
        <xs:attribute name="locid" type="xs:string" use="optional" />
      </xs:complexType>
    
      <xs:complexType name="TypedTextContent">
        <xs:complexContent>
          <xs:extension base="TextContent">
            <xs:attribute name="type" type="xs:string" use="optional" />
            <xs:attribute name="elementType" type="xs:string" use="optional" />
            <xs:attribute name="integer" type="xs:boolean" use="optional" />
            <xs:attribute name="mayBeNull" type="xs:boolean" use="optional" />
            <xs:attribute name="domElement" type="xs:boolean" use="optional" />
            <xs:attribute name="elementInteger" type="xs:boolean" use="optional" />
            <xs:attribute name="elementDomElement" type="xs:boolean" use="optional" />
            <xs:attribute name="elementMayBeNull" type="xs:boolean" use="optional" />
          </xs:extension>
        </xs:complexContent>
      </xs:complexType>
    
      <xs:complexType name="NamedTypedTextContent">
        <xs:complexContent>
          <xs:extension base="TypedTextContent">
            <xs:attribute name="name" type="xs:string" use="required" />
          </xs:extension>
        </xs:complexContent>
      </xs:complexType>
    
      <xs:complexType name="ParamNamedTypedTextContent">
        <xs:complexContent>
          <xs:extension base="NamedTypedTextContent">
            <xs:attribute name="optional" type="xs:boolean" use="optional" />
          </xs:extension>
        </xs:complexContent>
      </xs:complexType>
    
    </xs:schema>
    

    第2版: 下面是我使用VS intellisense支持的标记编写的一些代码的示例:

    function getDottedProperty(obj, dottedPropertyChain) {
        ///<summary>
        ///  <para>
        ///    Returns the property of the object given by the dotted property
        ///     chain.
        ///  </para>
        ///  <para>
        ///    Example:
        ///      GetDottedProperty(
        ///          { a: { b: { c: { d: 'hello' } } } },
        ///          'a.b.c.d'
        ///      )
        ///  </para>
        ///</summary>
        ///<param name="obj" type="Object">Object</param>
        ///<param name="dottedPropertyChain" type="String">
        ///  The string of properties to access.
        ///</param>
    
        var propertySplits = dottedPropertyChain.split('.');
        while (propertySplits.length) {
            obj = obj[propertySplits.shift()];
        }
    
        return obj;
    }