代码示例:
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
我希望在this.bar
原型方法中理解智能感知Foo.baz
。
这是IDE限制吗?有没有解决方法?我的对象构造函数应该做些什么吗?
答案 0 :(得分:1)
我想出了一个解决方法 - 我对
不满意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();
}
答案 1 :(得分:1)
我知道这个问题已经过时了,但是这个模式对我来说对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
}
});