[英]Javascript: Why does vscode's autocompletion not find object-specific suggestions in some cases?
I'm trying to understand why vsCode's autocomplete function will sometimes give me a list of all members of a certain object and sometimes only a short list of options that I have used before but have nothing to do with the object in question, as seen in the images below.我试图理解为什么 vsCode 的自动完成 function 有时会给我一个特定 object 的所有成员的列表,有时只是我以前使用过的选项的简短列表,但与问题中的 ZA8CFDE6331BD59EB2AC96F8 中看到的 ZA8CFDE6331BD59EB2AC96F8 无关,下面的图片。
For me as a beginner it's so very important to be able to explore the Document Object Model with the help of visual suggestions and from my limited understanding I just can't spot any difference between the two examples I'm looking at.对于我作为初学者来说,能够在视觉建议的帮助下探索文档 Object Model 非常重要,并且根据我有限的理解,我无法发现我正在查看的两个示例之间的任何区别。 The code below is all that there is in the current file (for simplicity):
下面的代码就是当前文件中的所有代码(为简单起见):
const myCanvas = document.getElementById("canvas");
const myContext = myCanvas.getContext("2d");
From myCanvas.
来自
myCanvas.
I would get all suggestions as expected, like "getContext", whereas from myContext.
我会按预期获得所有建议,例如“getContext”,而来自
myContext.
I only get an assortment of some items I've used before, but none of the methods or fields of the renderingContext2d-object, like "beginPath".我只得到了我以前使用过的一些项目的分类,但没有得到renderingContext2d-object的方法或字段,比如“beginPath”。 Why is that and is there anything I can do to get all suggestions?
为什么会这样,我能做些什么来获得所有建议?
vsCode with incomplete autocomplete suggestions具有不完整自动完成建议的 vsCode
VsCode with complete autocomplete suggestions带有完整自动完成建议的 VsCode
const myContext = myCanvas.getContext("2d");
The type of the return value of this function is based on the parameter so vscode doesn't actually know what type "myContext" is.这个 function 的返回值的类型是基于参数的,所以 vscode 实际上并不知道“myContext”是什么类型。 So it can't auto suggest anything.
所以它不能自动建议任何东西。
While with同时与
const myCanvas = document.getElementById("canvas");
The function always returns a DOM element type so vscode can auto suggest DOM element methods and properties. function 始终返回 DOM 元素类型,因此 vscode 可以自动建议 DOM 元素方法和属性。
You can use JSDoc style type annotations , but you should know which type of element you'll get from document.getElementById.您可以使用JSDoc 样式的类型注释,但您应该知道您将从 document.getElementById 获得哪种类型的元素。 In your example this type is HTMLCanvasElement, what you can define like that:
在您的示例中,此类型是 HTMLCanvasElement,您可以这样定义:
/** @type {HTMLCanvasElement} */
const myCanvas = document.getElementById("canvas");
/** @type {CanvasRenderingContext2D} */
const myContext = myCanvas.getContext("2d");
After that, myCanvas.get... will give you "getContext" hint.之后,myCanvas.get... 会给你“getContext”提示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.