- Is it possible to change the default getter/setter for an object's key-like properties? (The ones that don't have descriptors.)
- What is the best way to tell if a property is dynamic/doesn't have descriptors? [I give a way below.]
- If we are calling properties with descriptors, 'dynamic', then what do we call key-like properties (that don't have descriptors)? The opposite of dynamic is static, but 'static' has a specific meaning re. class/instance properties.
Some example code for properties:
Code: Select all
;non-existent properties v. keys:
oMap := Map()
;MsgBox(oMap["key"]) ;Error: Key not found.
MsgBox(oMap.prop) ;(blank)
;assigning properties v. keys:
oMap := Map()
MsgBox(ObjOwnPropCount(oMap) " " oMap.Count) ;0 0
Loop 65535
vChar := Chr(A_Index)
, oMap.%vChar% := 1 ;property
, oMap[vChar] := 1 ;key
MsgBox(ObjOwnPropCount(oMap) " " oMap.Count) ;65509 65535
;call method dynamically without %%:
oMap := Map(StrSplit("k1,v1,k2,v2,k3,v3,k4,v4,k5,v5", ",")*)
MsgBox(oMap.Count) ;5
vMtd := "Delete"
oMap.GetMethod(vMtd).Call(oMap, "k3")
MsgBox(oMap.Count) ;4
;call property dynamically without %%:
oMap := Map(StrSplit("k1,v1,k2,v2,k3,v3,k4,v4,k5,v5", ",")*)
vProp := "Count"
vCount := Map.Prototype.GetOwnPropDesc(vProp).Get.Call(oMap)
MsgBox(vCount) ;5
;get/set property value dynamically without %%:
oMap := Map(StrSplit("k1,v1,k2,v2,k3,v3,k4,v4,k5,v5", ",")*)
vProp := "k6"
Map.Prototype.GetOwnPropDesc("__Item").Set.Call(oMap, "v6", vProp)
MsgBox(oMap.Count) ;6
vProp := "k3"
vValue := Map.Prototype.GetOwnPropDesc("__Item").Get.Call(oMap, vProp)
MsgBox(vValue) ;v3
;get property info (non-existent/simple key-like/dynamic):
oMap := Map()
;prop0 is undefined
oMap.prop1 := "value1"
oMap.DefineProp("prop2", Object())
for _, vProp in ["prop0", "prop1", "prop2"]
{
if oMap.HasOwnProp(vProp)
{
try
ObjOwnPropCount(oMap.GetOwnPropDesc(vProp))
, vType := "D"
catch
vType := "S"
}
else
vType := "_"
vOutput .= vType " " vProp "`r`n"
}
MsgBox(vOutput)
The changes are subtle, yet significant, highlighted by the use of asterisks 8 times below.
AHK v1 basic objects:
case-insensitive keys
case-insensitive properties (with getters/setters)
sharing the same namespace
non-existent keys/properties return a blank string
AHK v2 maps:
case-*sensitive* keys
case-*insensitive* properties (with getters/setters, *and key-like*)
with *separate* namespaces
(the storage space is effectively *doubled*)
(you can have a regular map, but with *special info* stored in properties, e.g. values or objects)
non-existent keys cause an *error* (if an attempt is made to read from one)
non-existent properties return a *blank string*