自定义代码定义/派生定义的Microsoft Visual Studio对象中的Javascript Intellisense
情况:使用函数声明类 如果您像WinJs一样使用和声明带有某些自定义(或框架函数)的类(检查它们的开源git目录),那么您肯定熟悉此类代码: 函数定义(构造函数、instanceMembers、staticMembers){} 函数派生(基类、构造函数、instanceMembers、staticMembers){} 这些“自定义”功能的常见问题 当您试图从原型函数获取构造函数中声明的值时,Intellisense不会显示这些值 我发现了一些对我有帮助的东西: 这让我找到了下面我与大家分享的解决方案,让它工作起来是一件痛苦的事情,事实上,我正要**再次**放弃这个问题,这是一件非常令人失望的事情,尤其是在大型团队项目中。 我觉得奇怪的是,网络上没有太多关于这个的抱怨,也许是配置问题?然而,我看到的所有VSD安装都存在这个问题自定义代码定义/派生定义的Microsoft Visual Studio对象中的Javascript Intellisense,javascript,visual-studio,object,intellisense,Javascript,Visual Studio,Object,Intellisense,情况:使用函数声明类 如果您像WinJs一样使用和声明带有某些自定义(或框架函数)的类(检查它们的开源git目录),那么您肯定熟悉此类代码: 函数定义(构造函数、instanceMembers、staticMembers){} 函数派生(基类、构造函数、instanceMembers、staticMembers){} 这些“自定义”功能的常见问题 当您试图从原型函数获取构造函数中声明的值时,Intellisense不会显示这些值 我发现了一些对我有帮助的东西: 这让我找到了下面我与大家分享的解决
因此,如果您遇到同样的情况,我希望下面的解决方案也能对您有所帮助。几个小时后,我终于找到了一个不完美的解决方案(我已经在javascript库中处理了.base,就像C中一样,但使用下面的代码,我不能对intellisense说这是“.base(…)”存在于原型函数和构造函数的上下文中)。如果你有任何关于如何做的建议,让我知道,我很感兴趣 在Visual Studio 2013上测试
- 只需将window.define/window.deriver更改为实际使用的名称空间和名称(对于WinJs,它将是WinJs.Class.define和WinJs.Class.deriver)
- Add-in _references.js文件的相对路径,您将在库的后面放置以下代码
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
}
}
(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);