我正在做一些测试,在javascript中将值转换为整数,并在遇到这种奇怪的行为时在控制台中打印输出.
console.log(+[]) ==> 0
console.log(+[123]) ==> 123
console.log(+['123']) ==> 123
console.log(+[123, 456]) ==> NaN
console.log(+['123asdf']) ==> NaN
Run Code Online (Sandbox Code Playgroud)
我认为这些值是使用parseInt转换的,但结果却不是这样我去了javascript转换表http://www.w3schools.com/js/js_type_conversion.asp
这让我更好地了解如何执行转换.根据此表
[] => 0
[20] => 20
[10,20] => NaN
["twenty"] =>NaN
["ten","twenty"] => NaN
Run Code Online (Sandbox Code Playgroud)
因此,他们显然采用数组的第一个值并使用指定的规则进行转换.parseInt的规则不适用.
我测试得出这个结论.您可以将所有内容嵌套,它会给您相同的结果.
console.log(+[[[[[[[[[[[10]]]]]]]]]]]) => 10
Run Code Online (Sandbox Code Playgroud)
那么我想,好吧,如果是这样的话
console.log(+[undefined]) will return NaN
console.log(+[null]) will return 0
console.log(+[false]) will return 0
Run Code Online (Sandbox Code Playgroud)
这些是从javascript转换表到整数的预期值,但结果是
console.log(+[undefined]) => 0
console.log(+[null]) => 0
console.log(+[false]) => NaN
Run Code Online (Sandbox Code Playgroud)
最后一个是最奇怪的,因为false被转换为0,而不是NaN.有人可以解释这种奇怪的行为或解释如何执行此转换吗?
我正在尝试将JavaScript的单元测试添加到我的网站中.我使用VS2013,我的项目是一个ASP.NET网站.
基于建议(http://www.rhyous.com/2013/02/20/creating-a-qunit-test-project-in-visual-studio-2010/)我到目前为止做了:
写了简单的测试:
test( "Trainer should have non-empty group", function () {
var group = "group";
var trainer = new Trainer(123, "Name123", group, 123);
EQUAL(trainer.getTrainerGroup(), group);
});
Run Code Online (Sandbox Code Playgroud)注意:我的trainings.js文件包含
function Trainer(id, name, group, level) {
...
var _group = group;
this.getTrainerGroup = function () { return _group ; }
};
Run Code Online (Sandbox Code Playgroud)
当我执行我的测试时,我看到错误:未定义培训师.
它看起来像是我的班级无法识别.我觉得链接文件还不够,但我错过了什么?
请帮助添加对类和运行单元测试的原始文件的引用.
谢谢.
PS问题2:我可以添加对2个文件的引用(我的单元测试还需要一个在另一个文件中的类)吗?怎么样?
我试图创建一个简单的加载器动画来回绘制一条线,但目前只在一个方向上移动.一旦到达动画的中间,它就不会在相反方向上制作动画.
这是我的CSS
@keyframes loader-animation {
0% {
width: 0%;
}
49% {
width: 100%;
}
50% {
left: 100%;
}
100% {
left: 0%;
width: 100%
}
}
.loader {
height: 5px;
width: 100%;
}
.loader .bar {
position: relative;
height: 5px;
background-color: dodgerblue;
animation-name: loader-animation;
animation-duration: 3s;
animation-iteration-count: infinite;
animation-timing-function: ease-in-out;
}
Run Code Online (Sandbox Code Playgroud)
而我的HTML
<div class="loader">
<div class="bar"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
还有一个代码的jsfiddle
有人能告诉我我做错了什么吗?