【问题标题】:How do I create a function prototype in powershell?如何在 powershell 中创建函数原型?
【发布时间】:2016-12-02 22:12:04
【问题描述】:

我找不到示例。如何为我的 powershell 函数制作原型?

function exampleFunc(); //other stuff that calls the example function Function exampleFunc(){//stuff}

【问题讨论】:

  • SO Docs上有例子
  • 我的意思是喜欢function exampleFunc(); other stuff that calls the example function Function exampleFunc(){//stuff}
  • 你不能那样做。 Powershell 不是编译语言。

标签: powershell function-prototypes


【解决方案1】:

这就是@bacon-bits 代码的样子

function main()
{
    cls
    Begin
    Process
    End
}

function  Begin {

}

function  Process {

}

function  End {

}

main

【讨论】:

  • 对函数进行原型设计对 powershell 来说是一个非常有用的功能。我不想考虑按功能排序以供使用。
【解决方案2】:

PowerShell 不支持原型函数、前向声明或您想为此使用的任何术语。在 PowerShell 中,当您使用 function 关键字时,您正在定义一个函数。如果你用相同的函数名调用它两次,你改变了函数的定义。

This question about the same issue with bash 列出了解决该问题的常用方法。您可以在 PowerShell 中执行相同的操作。

另一种选择是使用Begin {} Process {} End {} 高级函数构造,并将所有函数声明放在Begin {} 部分。

【讨论】:

    【解决方案3】:

    您可以通过右键单击或通过快速按钮 Ctrl+J 访问 Powershell ISE(集成脚本环境)调用 sn-ps。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-09
      • 1970-01-01
      • 1970-01-01
      • 2012-10-03
      • 2012-12-13
      • 1970-01-01
      • 2019-05-06
      • 2021-05-31
      相关资源
      最近更新 更多