JScript verwendet den JavaScript-Funktionssatz as it existed in IE8. Auch unter Windows 10 ist der Windows Script Host auf JScript 5.7 beschränkt. Diese MSDN documentation erklärt:
Starting with JScript 5.8, by default, the JScript scripting engine supports the language feature set as it existed in version 5.7. This is to maintain compatibility with the earlier versions of the engine. To use the complete language feature set of version 5.8, the Windows Script interface host has to invoke IActiveScriptProperty::SetProperty .
... was letztlich bedeutet, da cscript.exe
und wscript.exe
keine Schalter haben so dass Sie diese Methode aufzurufen, Microsoft berät Sie Ihr eigenes Skript Host zu schreiben, um das Chakra Motor zu entsperren.
Es gibt jedoch eine Problemumgehung. Sie können das COM-Objekt htmlfile
aufrufen, es zu IE9-Kompatibilität (oder 10 oder 11 oder Edge) zwingen und dann die gewünschten Methoden importieren - einschließlich Array.forEach()
, JSON-Methoden und so weiter. Hier ist ein kurzes Beispiel:
var htmlfile = WSH.CreateObject('htmlfile');
htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=9" />');
// And now you can use htmlfile.parentWindow to expose methods not
// natively supported by JScript 5.7.
Array.prototype.forEach = htmlfile.parentWindow.Array.prototype.forEach;
Object.keys = htmlfile.parentWindow.Object.keys;
htmlfile.close(); // no longer needed
// test object
var obj = {
"line1" : "The quick brown fox",
"line2" : "jumps over the lazy dog."
}
// test methods exposed from htmlfile
Object.keys(obj).forEach(function(key) {
WSH.Echo(obj[key]);
});
Ausgang:
The quick brown fox
jumps over the lazy dog.
Es gibt ein paar andere Methoden demonstrated in this answer-JSON.parse()
, String.trim()
und Array.indexOf()
.