No, you can't push ahk-objects to js-objects, these are completely different objects. The only way is to turn ahk-object to the js-like string, and then push this string to js-object in the js context, eg:
Code: Select all
script =
(
var JS_ArrayOfObjects = [
{ id: 1, color: "green" } ,
{ id: 2, color: "yellow"}
] ;
)
JS := GetJS()
JS.eval(script)
AHK_NewObject := { id: 3, color: "red" }
jsStr := ObjToString(AHK_NewObject)
JS.eval("JS_ArrayOfObjects.push(" . jsStr . ")")
JS.eval("alert(JS_ArrayOfObjects[2].color)")
GetJS() {
static doc := ComObjCreate("htmlfile")
, _ := doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=11"">")
, JS := doc.parentWindow
Return JS
}
ObjToString(obj) {
if IsObject( obj ) {
isArray := true
for key in obj {
if IsObject(key)
throw Exception("Invalid key")
if !( key = A_Index || isArray := false )
break
}
for k, v in obj
str .= ( A_Index = 1 ? "" : "," ) . ( isArray ? "" : """" . k . """:" ) . ObjToString(v)
Return isArray ? "[" str "]" : "{" str "}"
}
else if !(obj*1 = "" || RegExMatch(obj, "\s"))
Return obj
for k, v in [["\", "\\"], [A_Tab, "\t"], ["""", "\"""], ["/", "\/"], ["`n", "\n"], ["`r", "\r"], [Chr(12), "\f"], [Chr(08), "\b"]]
obj := StrReplace( obj, v[1], v[2] )
Return """" obj """"
}