function first_function() {
var json = { a: 0, b: 1 };
second_function(json);
}
function second_function(json) {
// Pressing ctrl+enter to start intellisense
json.[ctrl+enter] // No intellisense, properties a and b won't show
}
嘿,所以我注意到 Visual Studio Code 中的这个奇怪的事情,其中我有一个 JavaScript 函数,例如我的示例中的 first_function ,其中创建了一个 JSON 变量并将其传递给 第二个函数。
问题:在第二个函数中,当我尝试启动 JSON 智能感知时,我没有显示属性 a 和 b。发生了什么?有什么办法可以解决这个问题吗?我的 VSC 是否配置不当还是什么?
最佳答案
just because you name a variable the same name(json) as function argument(json) it doesn't mean VSCode can deduce the type of the function argument. Try and use js-doc in your code to hint types https://github.com/Microsoft/TypeScript/wiki/JsDoc-support-in-JavaScript - mpm
这个答案非常有效,非常感谢!但该解决方案不切实际且冗长。这是一个采用 Google Apps 脚本 (GAS) 和 ECMAScript 5 (ES5) 的项目,并使用 Visual Studio Code 进行编辑和类型检查。
完成这项工作的方法是这样的......
/**
* This long note to get intellisense for an object
* @param {Object} json
* @param {Number} json.a
* @param {Number} json.b
*/
function second_function(json) {
json.[Start Intellisense] //A AND B ARE THERE YAY
}
作为一个解决方案,因为 Google Apps 脚本太愚蠢了,我最终用我需要的对象制作了 var 构造函数。 GAS 不一定能创建一个类,因为使用 var json = {}
然后为其创建一个很长的 JSDoc 感觉不值得付出努力。
这更实用
/**
* Sets up the record I want to build
* @param {Array} input
*/
function Json2Construct(input) {
/** @type Number */
this.a = input[0];
/** @type Number */
this.b = input[1];
};
/**
* Creates the record and sends to next function
*/
function first_function() {
var input = [0, 1];
var json = new Json2Construct(input);
second_function(json);
}
/**
* Will have intellisense this time too
* @param {Json2Construct} json
*/
function second_function(json) {
json.[Start Intellisense] // Woo a and b are also there!
}
非常感谢您的帮助,非常感谢!
关于javascript - Visual Studio Code JSON 对象在新函数中丢失智能感知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51829694/