可变范围和qUnit测试
Variable scope and testing with qUnit
我是qUnit测试的新手,很难理解为什么我的变量"a"通过了范围测试,即使在定义"a"之前调用了测试。当我注销"a"时,它的行为与预期的一样。有人能给我一个提示吗?
这是代码:
function outer() {
test('inside outer', function (assert) {
assert.equal(typeof inner, "function", "inner is in scope"); //pass
assert.equal(typeof a, "number", "a is in scope"); // pass
});
console.log(a); // undefined
var a = 1;
console.log(a); // 1
function inner() {}
var b = 2;
if (a == 1) {
var c = 3;
}
}
outer();
由于JavaScript的提升,"a"实际上是在函数的顶部声明的,但在代码中为其赋值的地方初始化。
所以,当你的代码被解释时,它实际上看起来更像这样:
function outer() {
var a, b, c;
test('inside outer', function (assert) {
assert.equal(typeof inner, "function", "inner is in scope"); //pass
assert.equal(typeof a, "number", "a is in scope"); // pass
});
console.log(a); // undefined
a = 1;
console.log(a); // 1
function inner() {}
b = 2;
if (a == 1) {
c = 3;
}
}
此外,请查看JavaScript的函数范围规则:http://www.w3schools.com/js/js_scope.asp
相关文章:
- QUnit 测试错误与 IE(Internet Explorer)
- 是否可以模拟 qUnit 测试的窗口位置对象
- 使用QUnit测试项是否具有某个类
- 在 Qunit 测试中使用 Zend 部分视图帮助程序
- 不能在 QUnit 测试中使用从 http url 引用的 jQuery
- 在烧瓶下运行 Qunit 测试的最佳方法
- 使用 qunit 测试我的代码时出错
- 如何使用 qunit 测试 html 元素的内容
- jQuery 焦点事件似乎在 qUnit 测试中不起作用
- 为什么这个 QUnit 测试失败
- 如何使用 QUnit 测试 JavaScript 错误恢复
- 使用 phantomjs 运行函数 qUnit 测试
- Qunit测试属性存在于对象中
- grunt没有在phantom上运行QUnit测试
- 在PhantomJS中从Ant运行异步QUnit测试
- Firefox 23中的QUnit测试:;类型错误:document.setUserData不是函数;
- 可变范围和qUnit测试
- QUnit测试间歇性失败
- 任何使用qunit测试异步jquery插件的方法,避免嵌入"start()"并保持异步
- 使用qunit测试javascript函数