- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写 JSCript 并使用 WindowsScriptHost 运行它。但是,它似乎缺少 Array.forEach()。
['a', 'b'].forEach(function(e) {
WSH.Echo(e);
});
因“test.js(66, 2) Microsoft JScript 运行时错误:对象不支持此属性或方法”而失败。
这不是对的吗?它真的缺少 Array.forEach() 吗?我真的必须使用其中一种 for 循环变体吗?
最佳答案
JScript 使用 JavaScript 功能集 as it existed in IE8 .即使在 Windows 10 中,Windows Script Host 也仅限于 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 建议您编写自己的脚本主机解锁脉轮引擎。
不过,有一个解决方法。您可以调用 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()
。
关于jscript - JScript/WindowsScriptHost 是否缺少 Array.forEach()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36497561/
我编写 JSCript 并使用 WindowsScriptHost 运行它。但是,它似乎缺少 Array.forEach()。 ['a', 'b'].forEach(function(e) {
我是一名优秀的程序员,十分优秀!