最近从基础开始学习JS, 被其中的动态类型转换弄得受不了了, 于是写了Jsquirks
(好吧, 我承认是我在参加Elance的JS Skill Test时被里面的题玩了, 于是我才决定重基础开始研究JS的.)
研究JS的动态类型转换时, 总是试图去找其中的模式, 结果往往自以为发现了一个能用规则, 结果一眨眼就会蹦出个反例. 于是就写了段代码用穷举的方式列出各种变态的特殊值与各种值的可能组合, 同时列出其值.
后来干脆做成了网页版的, 可自由选择用于组合的operand
与operator
.
github发布, 而后上线. 与大家分享. 对JS有兴趣的同学可以去逛逛. 各种希奇古怪的计算结果啊, 我表示再也不会试图去总结什么固定模式了.
好吧, 贴上才写的README.md
:
Javascript Quirks with special values and Operations
Demo online: http://jsquirks.zhuboliu.me
Do What
The App combinate the special values and values you selected to simple expressions, run it with the function eval
, and display the list of outputs in a table and logged it in the console. By using this, you can find some weired pattern in javascript value type converting.
Special Values
var special_values = [NaN, null, undefined, 0, 1, 2, true, false, Infinity, -Infinity, " ", "a"];
Operators
var arithmatic_operators = ["+", "-", "*", "/", '%'];
var comparison_operators = [">", ">=", "<", "<="];
var logical_operators = ["&&", "||"];
var equality_operators = ["==", "!=", "===", "!=="];
var bitwise_operators = ["<<", "<<<", ">>", ">>>"];
var unary_operators = ['typeof', 'void', '+', '-', '~', '!', '!!'];
var operators = {
arithmatic: arithmatic_operators,
comparison: comparison_operators,
logical: logical_operators,
equality: equality_operators
}
You can add some other operations such as bitwise operaters to the list.
Why am I doing this?
As you know, Javascript will dynamically convert value type in different situations;
The type converting is very weird. I tried to find the inner-rule of dynamic type converting, but I found nothing, It seems that there is no hard and fast rule at all. Every time I found a rule in one place, I found it was wrong in another place later.
At last, I write this simple tool to help me find the rules, of cause it’s useless. But with this tool I found some quirks I never found before. Hope this will help.
If You are preparing for some javascript quiz ,for example Elance skill test, this may help. If you found something wrong in the output, please send me a email or open an issue. If you found some hard and fast rule, please pull request.
Resources
- CSS Framework: http://getbootstrap.com
- View Template: http://getbootstrap.com/examples/dashboard/
- JS Libary:
- angularjs: http://angularjs.org
- checklist-model: http://vitalets.github.io/checklist-model/
TODO
- add some resourses helpful for understanding the value type converting.
- add bitwise operators
- add
new
to operators
NB , 学习了,发现好多东西以前都没有想过,或者注意到,多谢分享!
@lonso , 我觉得Infinity
才是匪夷所思呢. 比如说:
typeof Infinity //"number"
Math.max() //-Infinity
Math.min() //Infinity
Infinity + -Infinity //NaN
Infinity > 'a' //false
Infinity < 'a' //false
0 && Infinity //0
1 && Infinity //1
true && Infinity //Infinity
cool
@suffering 这一点不匪夷所思啊,js,或者说是数学的基础部分。 typeof Infinity 这个number就不说了 Math.max()方法是返回参数里面最大的,你参数一个都没有,那最大的就是负无穷 Math.min()同理 Infinity±Infinity 这在数学上也是无解的。学过高中数学的都知道,正无穷+正无穷=正无穷,负无穷+负无穷=负无穷,正负无穷相加得不到答案。 Infinity > ‘a’ Number类型无法和NaN进行比较,无论右边是什么NaN,都是false Infinity < ‘a’ 同理 js的&& 运算, 除非左边Boolean(左边的表达式返回值) ===false,否则右边一定会执行,并且整个&&返回的也是右边
@yuhj86 , 学习了 :) 话说我一直以为逻辑运算符返回的都一定是布尔值的.
1 && Infinity // 是 Infinity 不是 1
@idy , 1 && Infinity
的返回值确定是Infinity
, 我在 google chrome console 里反复确认了的. 看到我不明白的返回值, 我都会在console里手动运行查看结果.
珍惜生命,远离JS……
js的&& 运算, 除非左边Boolean(左边的表达式返回值) ===false,否则右边一定会执行,并且整个&&返回的也是右边
这条原则似乎是不对的吧.
0 && Infinity //0
1 && Infinity //1
true && Infinity //Infinity
@suffering 哪里不对了? 0 && Infinity 左边Boolean(0)–>false ,是false,右边不执行,不返回右边。 1 && Infinity 左边Boolean(1)–>true, 不是false,右边会执行,返回值为右边。 true && Infinity 左边Boolean(true)–>true,不是false,右边会执行,返回值为右边。
@suffering 你写错了 1 && Infinity 返回的应该是Infinity,不是1