If you didn’t read Part 1, I would recommend reading it as I build off of its functionality and theory.
Adding functions and variables to a PowerShell object is very useful, but
ScriptProperty objects give us some unique features (and gotchas) as well. We can attach them just as we did with other prototype helpers:
1 2 3 4 5 6 7 8 9 10 11 12
This kind of property comes in very handy for creating proxy properties, composite properties, and syntax helpers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
You MUST remember that
ScriptBlocks are not closures and you cannot capture variables with them! You can get into situations in which it appears that your code is working, but it is in fact accessing a variable from your current scope. The easiest mistake to make is using your
$prototype variable in a new function or
1 2 3 4 5 6 7 8 9 10 11 12 13 14
$target is defined, the
Value block has data to work with. Remember to always use $this to access members of your object within a
ScriptProperty. If you need to capture an argument, use a regular property (then optionally refer to it from the
1 2 3 4 5 6 7 8 9 10 11
ScriptProperty block you have to use the
$this automatic variable. From the about_Automatic_Variables help topic:
In a script block that defines a script property or script method, the $This variable refers to the object that is being extended.
In the case of our prototype,
$this refers to the underlying
PSObject allowing use to use functions, properties, and variables that we have attached and any others that the object provides.
If you want to read more, Part 3 is now available.