VS 2010 – 如何在my.Bar变量的原型方法中获取JavaScript Intellisense?

代码示例:

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

我想让Intellisense在Foo.baz原型方法中理解this.bar.

这是IDE限制吗?有没有解决方法?我的对象构造函数应该做些什么吗?

最佳答案 我想出了一个解决方法 – 我对此并不满意

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();
}

这样做的缺点是更多不必要的代码 – 我真的不明白为什么一个类应该总是被强制使用use访问器用于它的私有变量.

注意 – 尝试使用单功能Getter / Setter也没有让我到任何地方

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(); 
}
点赞