JScript использует набор функций JavaScript as it existed in IE8. Даже в Windows 10 хост Windows Script ограничен JScript 5.7. Это MSDN documentation объясняет:
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 .
... который в конечном счете означает, так как cscript.exe
и wscript.exe
не имеют переключателей, позволяющие вам вызывать этот метод, Microsoft советует вам написать свой собственный хост скрипта, чтобы разблокировать двигатель Чакры.
Существует обходное решение. Вы можете вызвать COM-объект , заставить его совместимость с IE9 (или 10 или 11 или Edge), а затем импортировать любые методы, которые вы хотите, включая Array.forEach()
, методы JSON и т. Д. Вот краткий пример:
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]);
});
Выход:
The quick brown fox
jumps over the lazy dog.
Есть несколько других методов demonstrated in this answer - JSON.parse()
, String.trim()
и Array.indexOf()
.