我可以将函数分配给变量吗? [英] Can I assign a function to a variable?

查看:47
本文介绍了我可以将函数分配给变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能将函数分配给变量是否正确 - 例如:

is it correct that I cannot assign a function to a variable - like:

     $h = {ID="x1";readStream=$Null;... }
     ...
     $h.readStream = function{ ... }

谢谢!

附言@ A.Wiechers:对我来说,如果所有的标题在第一次进入 prg.-langague 时都会有很大帮助,问题是关于!!对于这个问题,我必须只打开针提示,如果例如,我可以跳过这些提示.我可以将一个函数分配给一个变量以在另一个函数中使用吗?1"将以Python"开头 - 标签的条目不会过滤 StackExchange 的建议!这甚至有助于防止仅仅因为标题相似而出现重复条目​​!!

P.S. @ A.Wiechers: To me it would help a lot if ALL the Title has at first entry the prg.-langague the question is about!! For this question I have to open only needles hints which I would have been able to skip if e.g. "Can I assign a function to a variable for use within another function? 1" would start with "Python" - the entry of the tags do NOT filter the StackExchange-suggestions! This would even help to prevent duplicate entries just because the titles are similar!!

推荐答案

你只需要使用一个脚本块.

You just need to use a scriptblock.

让我们举一个非常简单的例子:我想在我的函数中将两个整数相乘:

let's take a really simple example : I want to multiply two integers in my function :

只需执行以下操作:

$h = @ {ID="x1" ; DoStuff = $Null }
$h.DoStuff = { $args[0] * $args[1] }
# note that $h = @{ID="x1" ; DoStuff = {$args[0] * $args[1]} } works as well


# Invoke your block : 
& $h.DoStuff 6 7

# or 
$h.DoStuff.Invoke(6,7)

这篇关于我可以将函数分配给变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆