locked
JavaScript intellisense with "this" inside getter/setter__

    Question

  • Take this sample code:

    (function () {
        "use strict";
    
        var baseClassWinJS = WinJS.Class.define(function(){
            intellisense.logMessage("Entre baseClassWinJS constructor");
        },
        {
            a: function () {
                intellisense.logMessage("TEST5");
                return 4;
            },
            b: function () {
                intellisense.logMessage("TEST6");
                return "you";
            },
            c: {
                get:function () {
                    for(var key in this){
                        intellisense.logMessage(key + "=" + this[key]);
                    }
                    this.  
    
                    // Intellisense give me "this.get()" 
                    // But I expected "this.a()" and "this.b()"...
                    // Here is the output of this method, which show my "this" to have an incorrect context...
    
                    //02:48:56.0792: get=function () {
                    //    for(var key in this){
                    //        intellisense.logMessage(key + "=" + this[key]);
                    //    }
                    //    this.
                    //    // Intellisense give me "this.get()" 
                    //    // But I expected "this.a()" and "this.b()"...
                    //    // Here is the output
                    //}
    
                },
            }
        });
    })();
    The problem I have is the getter / setter doesn't react well because the "this" is in the wrong context. Is there any patch / .intellisense.js file existing to support getter / setter intellisense?
    Friday, July 27, 2012 6:54 PM

Answers

  • This is due to the get never being called.  Without the get being called IntelliSense will have difficulty determining what the value for "this" will be.  IntelliSense can be improved in this case by calling the get as below:

    (function () {
        "use strict";
    
        var baseClassWinJS = WinJS.Class.define(function () { },
        {
            a: function () { return 4; },
            b: function () { return "you"; },
            c: {
                get: function () {
                    for (var key in this) { }
                    // type "this." here
                },
            }
        });
        (new baseClassWinJS()).c
    })();


    This posting is provided "as is" with no warranties, and confers no rights.


    • Edited by Bryan Caldwell Friday, July 27, 2012 7:50 PM
    • Marked as answer by Instriker Friday, July 27, 2012 8:37 PM
    Friday, July 27, 2012 7:38 PM

All replies

  • This is due to the get never being called.  Without the get being called IntelliSense will have difficulty determining what the value for "this" will be.  IntelliSense can be improved in this case by calling the get as below:

    (function () {
        "use strict";
    
        var baseClassWinJS = WinJS.Class.define(function () { },
        {
            a: function () { return 4; },
            b: function () { return "you"; },
            c: {
                get: function () {
                    for (var key in this) { }
                    // type "this." here
                },
            }
        });
        (new baseClassWinJS()).c
    })();


    This posting is provided "as is" with no warranties, and confers no rights.


    • Edited by Bryan Caldwell Friday, July 27, 2012 7:50 PM
    • Marked as answer by Instriker Friday, July 27, 2012 8:37 PM
    Friday, July 27, 2012 7:38 PM
  • Cool, it works well. Thanks to Visual Studio with the _references.js. With it, I just overwritten the default behavior of Define/Derive so it automatically construct new instances and invoke every members automatically.:

    (function (window) {
        "use strict";
    
        if (window.$winJsIntellisenseFixed) {
            return;
        }
        intellisense.logMessage("Overriding default WinJS.Class.define ...");
    
        window.$winJsIntellisenseFixed = true;
    
        var oldDefine = WinJS.Class.define;
        var oldDerive = WinJS.Class.derive;
       
        WinJS.Class.define = function (constructor, instanceMembers, staticMembers) {
            var result = oldDefine(constructor, instanceMembers, staticMembers);
            initIntellisenseFor(result);
            return result;
        };
    
        WinJS.Class.derive = function (baseClass, constructor, instanceMembers, staticMembers) {
            var result = oldDerive(baseClass, constructor, instanceMembers, staticMembers);
            initIntellisenseFor(result);
            return result;
        };
    
        function initIntellisenseFor(constructor) {
            var inst = new constructor();
            for (var key in inst) {
                inst[key];
            }
        }
    })(this);

    I think Microsoft could ship a fix into the "C:\Program Files (x86)\Microsoft Visual Studio 11.0\JavaScript\References" so we don't have to do this manually.

    Friday, July 27, 2012 8:59 PM