情况:使用函数声明您的类
如果像WinJs一样使用和声明带有某些自定义(或框架函数)的类(检查其开源git目录),则您肯定对这种代码很熟悉:
函数define(constructor,instanceMembers,staticMembers){}
函数派生(baseClass,构造函数,instanceMembers,staticMembers){}
define(function constructor(){
this.yourProperty = 1;
}, {
// Prototype object
somePrototypeFunction: function(){
// When you type "this." here, it will not show up "yourProperty" declared
// in the constructor, because you have not instanciated the class,
// intellisense does not know that everything is linked
}
}
这些“自定义”功能的常见问题
当您尝试从原型函数获取它们时,Intellisense不会显示在构造函数中声明的值。
我找到了对我有帮助的东西:http : //social.msdn.microsoft.com/forums/windowsapps/en-US/3eee400a-fefd-4f5e-9109-68df03fef006/javascript-intellisense-with-this-inside-gettersetter
这使我找到了我在下面分享给您的解决方案,使其无法正常工作,实际上我要**再次**放弃这个问题,这确实令人失望,尤其是对于大型团队项目。我觉得很奇怪,网络上对此没有太多抱怨,也许这是配置问题?但是,我在看到的所有VSD安装上都遇到了这个问题。
因此,如果遇到同样的情况,我希望以下解决方案也能对您有所帮助。
几个小时后,我终于有了一个不完美的解决方案(我在javascript库中像处理C#一样处理了.base,但是使用以下代码,我不能说出这是“ .base(...)”存在于原型函数和构造函数的上下文中)。如果您对如何操作有任何建议,请告诉我,我很感兴趣。
在Visual Studio 2013上测试。
只需将window.define / window.derive更改为您实际使用的名称空间和名称(对于WinJ,它将是WinJS.Class.define和WinJS.Class.derive)。
在_references.js中添加文件的相对路径,将以下代码放置在库之后
就这样!您的内心将会有智慧
(function (window) {
"use strict";
/*
* Goal: make intellisense understand that the constructor of your define/derive functions are linked to the prototype object you have supplied.
* Tested on WinJs library and other custom libraries.
* Save this in a file, and reference it only in _references.js, insert it after your library containing the define/derive functions
*/
function initIntellisenseFor(constructor, baseClass) {
var inst = new constructor();
// Force intellisense to run function
for (var key in inst) {
if (typeof inst[key] == 'function') {
try {
inst[key]();
} catch (e) {
// Silent fail if wrong arguments (not sure if needed)
}
}
}
// Force intellisense to discover constructor
inst.constructor = constructor;
// Missing: .base() implementation for each method with redirection to the appropriate parent class method
}
var oldDefine = window.define;
window.define = function (constructor, instanceMembers, staticMembers) {
var result = oldDefine.call(this, constructor, instanceMembers, staticMembers);
initIntellisenseFor(result);
return result;
};
var oldDerive = window.derive;
window.derive = function (baseClass, constructor, instanceMembers, staticMembers) {
var result = oldDerive.call(this, baseClass, constructor, instanceMembers, staticMembers);
initIntellisenseFor(result, baseClass);
return result;
};
})(this);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句