Created
July 27, 2012 21:49
-
-
Save idavis/3190634 to your computer and use it in GitHub Desktop.
PowerShell like JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$foo = @{ | |
Message = "This is a pre-recorded message" | |
} | |
$foo.say = { | |
param([string]$message) | |
$speaker = new-object -com SAPI.SpVoice | |
($speaker.Speak($message, 1)) | out-null | |
} | |
$foo.SayPreRecordedMessage = { . $foo.say $foo.Message } | |
#Then you can run: | |
. $foo.SayPreRecordedMessage | |
#but, we don't really have a factory, so let's fix that: | |
#fake class | |
function new-foo { | |
$foo = @{} | |
$Message = "This is a pre-recorded message" | |
# use this line if you want $message to be a member of foo | |
#$foo = @{Message = "This is a pre-recorded message" } | |
$foo.say = { | |
param([string]$message) | |
$speaker = new-object -com SAPI.SpVoice | |
($speaker.Speak($message, 1)) | out-null | |
} | |
$foo.SayPreRecordedMessage = { . $foo.say $Message } | |
$foo | |
} | |
$myFoo = new-foo | |
. $myfoo.SayPreRecordedMessage | |
. $myfoo.say "Hello, World!" | |
# we get new objects: | |
$foo2 = new-foo | |
$myFoo -eq $foo2 | |
#>False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
filter add-function {param([string]$name, [scriptblock]$value) $_[$name] = $value; $_ | Add-Member ScriptProperty $name {$this[$name]} } | |
function new-prototype { | |
$prototype = @{} | |
$prototype | |
} | |
$obj = new-prototype | |
$obj | add-function Foo {write-host "OMGHAI"} | |
$obj | add-function Bar {. $obj.Foo} | |
$obj.Bar.Invoke() | |
#OMGHAI | |
. $obj.Bar | |
#OMGHAI |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
filter add-function {param([string]$name, [scriptblock]$value) $_[$name] = $value; $_ | Add-Member ScriptProperty $name {$this[$name]} } | |
function new-prototype { | |
$prototype = @{} | |
$prototype = @{Message = "This is a pre-recorded message" } | |
$prototype.say = { | |
param([string]$message) | |
$speaker = new-object -com SAPI.SpVoice | |
($speaker.Speak($message, 1)) | out-null | |
} | |
$prototype | |
} | |
$obj = new-prototype | |
$obj.SayPreRecordedMessage = { . $obj.say $obj.Message } | |
$obj | add-function SaySomethingElse {. $obj.say "And now for something completely different"} | |
. $obj.SayPreRecordedMessage | |
. $obj.say "Hello, World!" | |
. $obj.SaySomethingElse | |
$obj.SaySomethingElse.Invoke() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
filter add-function { | |
param( | |
[string]$name, | |
[scriptblock]$value | |
) | |
$method = new-object System.Management.Automation.PSScriptMethod "$name", $value | |
$_.psobject.members.add($method) | |
} | |
function new-prototype { | |
$prototype = new-object psobject @{Message = "This is a pre-recorded message" } | |
$prototype | add-function say { | |
param([string]$message) | |
$speaker = new-object -com SAPI.SpVoice | |
($speaker.Speak($message, 1)) | out-null | |
} | |
$prototype | |
} | |
$obj = new-prototype | |
$obj.say($obj.message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
filter new-function { | |
param( | |
[string]$name, | |
[scriptblock]$value | |
) | |
$method = new-object System.Management.Automation.PSScriptMethod "$name", $value | |
$_.psobject.members.add($method) | |
} | |
filter new-property { | |
param( | |
[string]$name, | |
[scriptblock]$getter, | |
[scriptblock]$setter = $null | |
) | |
$property = new-object System.Management.Automation.PSScriptProperty "$name", $getter, $setter | |
$_.psobject.properties.add($property) | |
} | |
filter new-autoproperty { | |
param( | |
[string]$name, | |
[object]$value | |
) | |
$variable = new-object System.Management.Automation.PSVariable $name, $value | |
$property = new-object System.Management.Automation.PSVariableProperty $variable | |
$_.psobject.properties.add($property) | |
} | |
function new-prototype { | |
param($baseObject) | |
$base = $baseObject | |
if($baseObject -eq $null){ $base = @{} } | |
$prototype = new-object psobject $base | |
$prototype | |
} | |
function new-sapivoice { | |
$prototype = new-prototype (new-object psobject) | |
$prototype | new-function say { | |
param([string]$message) | |
$speaker = new-object -com SAPI.SpVoice | |
($speaker.Speak($message, 1)) | out-null | |
} | |
$prototype | new-autoproperty Message1 "This is Message 1" | |
#$prototype.Message2 = "This is Message 2" # this only works if the base is a hash | |
$prototype | new-autoproperty Message3 "This is Message 3" | |
$prototype | new-property Message4 {"This is Message 4"} | |
#we can reference and rewrite Message1 using Message5 as a proxy | |
$prototype | new-property Message5 {$this.Message1} {param([String]$value); $this.Message1 = $value} | |
$prototype | |
} | |
$obj = new-sapivoice |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment