简体   繁体   English

这个打字稿 forEach 片段有什么作用?

[英]What does this typescript forEach snippet do?

When I am about to type forEach() in a typescript file the Visual Studio Code editor offers me this snippet当我要在打字稿文件中键入 forEach() 时,Visual Studio Code 编辑器为我提供了这个片段

在此处输入图片说明 and pastes in the following:并粘贴以下内容:

    let foo: any;
    
    // something gets assigned to foo...

    foo.array.forEach(element => {
        
    });

Why does it insert the array property and what is it?为什么它插入array属性,它是什么? In what scenario is this snippet intended to be used?此代码段打算在什么情况下使用?

UPDATE 2020: I see the foreach snippet that adds "array" is no longer offered by VSCode in this situation. 2020 年更新:我看到在这种情况下,VSCode 不再提供添加“数组”的 foreach 片段。 I guess I wan't the only person who found it confusing.我想我不是唯一一个感到困惑的人。

For that snippet you don't use yourArray.forEa... and then select the snippet.对于该片段,您不使用yourArray.forEa...然后选择片段。

You just use it directly as forEa... and when you click on it the full snippet gets created with array placeholder which you can immediately replace with the name of your array.您只需将它直接用作forEa... ,当您单击它时,将使用array占位符创建完整的代码段,您可以立即将其替换为数组的名称。

So what you did is you put your array at the beggining which is extra and not needed, and on that you appended a full snippet, which already has array placeholder.因此,您所做的是将数组放在额外且不需要的开头,并在其上附加了一个完整的片段,该片段已经具有数组占位符。


PS I think it would be more intuitive if the snippet was made the way you try to use it. PS 我认为如果片段是按照您尝试使用它的方式制作的,它会更直观。 This happens to me every time, so I have to delete the excessive array.我每次都会发生这种情况,所以我必须删除过多的array. that gets created.被创建。 But this is just my opinion.但这只是我的意见。

In Visual Studio Code editor you can also add your own snippets, that will be available as the others, by following File->Preferences(Code > Preferences on macOS)->User Snippets:Visual Studio Code editor您还可以添加自己的代码段,这些Visual Studio Code editor段将与其他Visual Studio Code editor段一样可用,方法是遵循 File->Preferences(Code > Preferences on macOS)->User Snippets: 在此处输入图片说明 Type in typescript.json (for this case) in input :inputinput typescript.json (对于这种情况): 在此处输入图片说明 Paste your snippet (if you would like to use it without automatically pasted property array ), and save:粘贴您的代码段(如果您想在不自动粘贴property array情况下使用它),并保存:

"forEach - my": {
    "prefix": "forEach",
    "body": [
        "forEach(element => {",
        "});" 
    ],
    "description": "My forEach"
}

forEach - my is the snippet name. forEach - my是片段名称。

prefix is how this snippet is selected from IntelliSense and tab completion. prefix是从 IntelliSense 和选项卡完成中选择此代码段的方式。

body is the content and either a single string or an array of strings of which each element will be inserted as separate line. body是内容和单个字符串或字符串数​​组,其中每个元素将作为单独的行插入。

description is the description used in the IntelliSense drop down. description是 IntelliSense 下拉列表中使用的描述。

More is here: https://code.visualstudio.com/docs/editor/userdefinedsnippets更多信息在这里: https : //code.visualstudio.com/docs/editor/userdefinedsnippets

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM