Reputation: 4966
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'd like to get Intellisense understanding this.bar
inside the Foo.baz
prototype method.
Is this a IDE limitation? Is there any workaround? Should my object constructors do something else?
Upvotes: 4
Views: 827
Reputation: 11
I know the question is old, but this pattern is working good for me with Intellisense, I can see all methods added with the extend, or other way. jQuery used a similar pattern to this, don't know if they still do
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
}
});
Upvotes: 1
Reputation: 4966
I've come up with one workaround - which I'm not very happy with
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, is a lot more unnecessary code - and I really don't see why a class should always be forced to use use accessors for its private variables.
Note - trying to have a single-function Getter/Setter didn't get me anywhere either
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();
}
Upvotes: 1