【问题标题】:Javascript Intellisense in Microsoft Visual Studio objects defined by custom code define / deriveMicrosoft Visual Studio 对象中的 Javascript Intellisense 由自定义代码定义/派生
【发布时间】:2014-09-17 21:30:01
【问题描述】:

情况:使用函数来声明你的类

如果你像 WinJs 那样使用和声明带有一些自定义(或框架函数)的类(检查他们的开源 git 目录),你肯定熟悉这种代码:

函数定义(构造函数,instanceMembers,staticMembers){}

函数派生(baseClass,构造函数,instanceMembers,staticMembers){}

define(function constructor(){
   this.yourProperty = 1;
}, {
   // Prototype object
   somePrototypeFunction: function(){
      // When you type "this." here, it will not show up "yourProperty" declared 
      // in the constructor, because you have not instanciated the class, 
      // intellisense does not know that everything is linked
   }
}

这些“自定义”函数的常见问题

当您尝试从原型函数访问构造函数中声明的值时,Intellisense 不会显示它们。

我发现了一些对我有帮助的东西:http://social.msdn.microsoft.com/forums/windowsapps/en-US/3eee400a-fefd-4f5e-9109-68df03fef006/javascript-intellisense-with-this-inside-gettersetter

这导致我找到了我在下面分享给你的解决方案,让它工作起来很痛苦,实际上我正要**再次**放弃这个问题,这确实令人失望,尤其是对于大团队项目。 我觉得很奇怪,网上没有很多关于这个的投诉,也许是配置问题?但是,我看到的所有 VSD 安装都有这个问题。

因此,如果您遇到相同的情况,我希望以下解决方案也能对您有所帮助。

【问题讨论】:

    标签: javascript visual-studio object intellisense


    【解决方案1】:

    几个小时后,我终于有了一个不完美的解决方案(我已经像在我的 javascript 库中的 C# 中一样处理了 .base,但是使用以下代码我不能告诉智能感知这个“.base(.. .)" 存在于原型函数和构造函数的上下文中)。如果您对如何做到这一点有任何提示,请告诉我,我很感兴趣。

    在 Visual Studio 2013 上测试。

    • 只需将 window.define / window.derive 更改为您实际使用的命名空间和名称(对于 WinJ,它将是 WinJS.Class.define 和 WinJS.Class.derive)。

    • 在 _references.js 中添加您将在其中放置以下代码的文件的相对路径,就在您的库之后

    仅此而已!你的

    里面会有智能感知
    (function (window) {
        "use strict";
        /*
         * Goal: make intellisense understand that the constructor of your define/derive functions are linked to the prototype object you have supplied. 
         * Tested on WinJs library and other custom libraries.
         * Save this in a file, and reference it only in _references.js, insert it after your library containing the define/derive functions
        */
    
        function initIntellisenseFor(constructor, baseClass) {
            var inst = new constructor();
            // Force intellisense to run function
            for (var key in inst) {
                if (typeof inst[key] == 'function') {
                    try {
                        inst[key]();
                    } catch (e) {
                        // Silent fail if wrong arguments (not sure if needed)
                    }
                }                
            }
            // Force intellisense to discover constructor
            inst.constructor = constructor;
    
            // Missing: .base() implementation for each method with redirection to the appropriate parent class method
        }
    
        var oldDefine = window.define;
        window.define = function (constructor, instanceMembers, staticMembers) {
            var result = oldDefine.call(this, constructor, instanceMembers, staticMembers);
            initIntellisenseFor(result);
            return result;
        };
    
        var oldDerive = window.derive;
        window.derive = function (baseClass, constructor, instanceMembers, staticMembers) {
            var result = oldDerive.call(this, baseClass, constructor, instanceMembers, staticMembers);
            initIntellisenseFor(result, baseClass);
            return result;
        };
    
    })(this);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-13
      • 1970-01-01
      • 2013-05-18
      • 1970-01-01
      相关资源
      最近更新 更多