不一致的“与”行为-没有访问功能-这是一个chrome的错误
Inconsistent `with` behavior - no access within functions - is this a chrome bug?
让我们暂时停止"不要使用with
"的合唱,并假设我有一个合法的用例*。如果我有这个代码
with({x: "hi"}) {
console.log("outside x is", x);
function aFunc() {
console.log("inside a func x is", x);
}
aFunc();
}
在IE11和Firefox中,这两个语句都打印"hi"。在chrome中,我在循环中得到x上的ReferenceError
。
哪个是正确的行为?这是chrome浏览器的bug吗?
*。用例是,我正在制作一个浏览器内IDE环境,允许用户在提供额外功能的同时输入javascript,并且不想将函数直接附加到全局作用域(想想jsbin/jsfiddle,但用于特定任务)。
哪个是正确的行为?
没有指定。问题是你的代码:你有一个with
块内的函数声明,这是无效的。函数声明必须是顶级程序/函数代码。
在Kangax的优秀网站上阅读更多关于块中的函数声明以及它们在不同浏览器中是如何处理的。
这是一个chrome bug吗?
。将代码更改为
with({x: "hi"}) {
console.log("outside x is", x);
var aFunc = function() {
console.log("inside a func x is", x);
}
aFunc();
}
一个IEFE也应该工作:
with({x: "hi"}) (function(){
console.log("outside x is", x);
function aFunc() {
console.log("inside a func x is", x);
}
aFunc();
}());
相关文章:
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- 如何通过自己获得Chrome扩展的用户反馈/错误报告
- 是什么让一个“;Uncaught RangeError:超过了最大调用堆栈大小“;错误(Chrome,在其他浏览器中显示
- chrome在WebSocket握手期间获取新错误
- 未捕获的类型错误:无法读取属性'addEventListener'的null chrome扩展名
- Chrome Javascript日期构造函数错误
- chrome.storage.sync未定义错误
- "资源不足错误“;当从Chrome控制台重复发出ajax请求时
- 加载Chrome扩展中的脚本错误
- 当与Dojo验证一起使用时,Chrome 41密码保存会做出错误的选择
- 仅打开调试器时出现Google chrome扩展错误
- Redux开发工具Chrome扩展Immutable.js导致错误
- Javascript代码在chrome和Firefox上给出错误
- 未捕获的语法错误:意外的令牌 = (Chrome)
- 尝试从我的 chrome 扩展程序访问 gmail 中的 iframe 时出现未定义的错误,但不是从开发者控制台访问
- JavaScript cookie和未捕获的语法错误:Chrome中出现意外的令牌u
- 在Firefox中出现404错误Chrome在IE上运行顺畅可能是什么原因
- 在控制台DOM中插入HTML代码后出现错误(Chrome浏览器)
- Javascript 未捕获的语法错误:Chrome 调试器中出现意外的标识符错误
- css上的滚动错误chrome/firefox