JScript/WindowsScriptHost 是否缺少 Array.forEach()?
Is JScript/WindowsScriptHost missing Array.forEach()?
我用 WindowsScriptHost.However 编写 JSCript 和 运行 它,它似乎缺少 Array.forEach()。
['a', 'b'].forEach(function(e) {
WSH.Echo(e);
});
失败 "test.js(66, 2) Microsoft JScript runtime error: Object doesn't support this property or method"。
这不会吧?它真的缺少Array.forEach()吗?我真的必须使用其中一种 for 循环变体吗?
JScript 使用 JavaScript 功能集 as it existed in IE8. Even in Windows 10, the Windows Script Host is limited to JScript 5.7. This 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 建议您编写自己的脚本宿主来解锁 Chakra 引擎。
不过,有一个解决方法。您可以调用 htmlfile
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()
。
我用 WindowsScriptHost.However 编写 JSCript 和 运行 它,它似乎缺少 Array.forEach()。
['a', 'b'].forEach(function(e) {
WSH.Echo(e);
});
失败 "test.js(66, 2) Microsoft JScript runtime error: Object doesn't support this property or method"。
这不会吧?它真的缺少Array.forEach()吗?我真的必须使用其中一种 for 循环变体吗?
JScript 使用 JavaScript 功能集 as it existed in IE8. Even in Windows 10, the Windows Script Host is limited to JScript 5.7. This 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 建议您编写自己的脚本宿主来解锁 Chakra 引擎。
不过,有一个解决方法。您可以调用 htmlfile
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()
。