VS 2010 - How can I get JavaScript Intellisense in my prototype methods for this.Bar variables?

Code example:

function Foo(){
  this.bar = "12345";
  //Intellisense works on this.bar
}

Foo.prototype.baz = function(){
  var bob = "12345";
  //Intellisense works on bob
  //Intellisense does not work on this.bar
}

var f = new Foo();
//Intellisense works on f.bar
//Intellisense works on f.baz

I would like to get an understanding of Intellisense this.barinside the prototype Foo.baz.

Is this an IDE limitation? Is there a workaround? Should my object constructors do something else?

+3
source share
2 answers

I came up with one workaround that I don't really like

function Foo(){
  this._bar = "12345";
  //Intellisense works on this.bar
}

Foo.prototype.setBar = function(bar){
    /// <param name="bar" type="String"/>
    if(bar){
        this._bar = bar;
    }
}
Foo.prototype.getBar = function(){
    /// <returns type="String"/>
    return this._bar;
}

Foo.prototype.baz = function(){
  var bob = "12345";

  //Intellisense works on bob
  //Intellisense works on this.getBar();
}

The disadvantages of this are much more unnecessary code - and I really don't understand why a class should always be forced to use accessor for its private variables.

Note. If you try to have a single function, Getter / Setter will not go anywhere.

Foo.prototype.bar = function(bar){
    // If a value is passed, set this._bar and return, otherwise, act as a getter
    if(bar){
        this._bar = bar;
    }
    return this._bar;
}
Foo.prototype.baz = function(){
  //Valid Javascript, Intellisense works
  this.bar("12345");  

  //Valid Javascript, Intellisense does not work
  //VS seems to do parameter checking...
  this.bar(); 
}
+1
source

, , Intellisense, , . jQuery , ,

var Foo = function (str) {
    return new Foo.fn.__construct(str);
};

// fn is just shorter than prototype
Foo.fn = Foo.prototype = {
    __construct: function (str) {
        // Intialization
        this.bar = "12345";
        // absolutely works
    },
    bar: "",
    extend: function (objectLiteral) {
        // Add to the prototype
        for (var k in objectLiteral) {
            this[k] = objectLiteral;
        }
    }
};

Foo.fn.__construct.prototype = Foo.fn;
Foo.extend = Foo.fn.extend;

Foo.fn.baz = function () {
    this.bar; // Intelli Works! 
};
Foo.fn.extend({
    baz: function () {
        this.bar;
        // Intelli doesnt work
    }
});
+1

Source: https://habr.com/ru/post/1782553/


All Articles