簡體   English   中英

由自定義代碼定義/定義的Microsoft Visual Studio對象中的Javascript Intellisense

[英]Javascript Intellisense in Microsoft Visual Studio objects defined by custom code define / derive

情況:使用函數聲明您的類

如果您像WinJs一樣使用和聲明帶有某些自定義(或框架函數)的類(請檢查其開源git目錄),則您肯定對這種代碼很熟悉:

函數define(constructor,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庫中像處理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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM