你真的了解JavaScript吗?
让我们重温一下JavaScript的一些基础知识,请先写出以下代码中问号处的答案,再运行比较!
<script type="text/javascript">
var a=0,b=0,c=0;
a+++b+++c;
alert([a,b,c]);//?
var a=[];
alert(a[a++]);//?
alert(a);//?
var o={a:o++};
alert(o.a);//?
var f=function(){};
alert(f(f++));
alert(typeof(NaN))//?
alert(typeof(Infinity))//?
alert(typeof(null))//?
alert(typeof(undefined))//?
alert(NaN == NaN)//?
alert(NaN != NaN)//?
alert(NaN >= NaN)//?
alert(null == undefined)//?
alert(null >= undefined)//?
alert(null <= undefined)//?
alert(parseInt("123abc"))//?
alert(("123abc" - 0))//?
alert(Infinity > 10)//?
alert(Infinity > "abc")//?
alert(Infinity == NaN)//?
alert(true == 1)//?
alert(new String("abc") == "abc")//?
alert(new String("abc") === "abc")//?
var a = "123abc";
alert(typeof(a++));//?
alert(a);//?
var a = "123abc";
a.valueOf = function(){return parseInt(a);}
alert(++a);//?
alert(a-0);//?
var a = new Object();
a.toString = function(){return "123abc";}
a.valueOf = function(){return parseInt(a);}
alert(++a);//?
alert(a-0);//?
var spv = String.prototype.valueOf;
String.prototype.valueOf = function(){
return parseFloat(this);
}
alert("123abc" > 122);//?
alert(new String("123abc") > 122);//?
String.prototype.valueOf = spv;
var s = new String("abc");
alert(typeof(s) == typeof("abc"));//?
alert(s === "abc");//?
alert(s.toString() == s);//?
var a = new Object(),b = new Object();
a.toString = function(){return "a"};
b.toString = function(){return "b"};
alert(a>b);//?
a.valueOf = function(){return 1};
b.valueOf = function(){return 0};
alert(a>b);//?
var step = function(a){
return function(x){
return x + a++;
}
}
var a = step(10);
var b = step(20);
alert(a(10));//?
alert(b(10));//?
</script>
运行代码查看结果:http://www.jslab.org.cn/?tag=javascriptBasis
希望大家在学习JavaScript的时候注意基本功的练习!
原文地址:http://www.v-ec.com/dh20156/article.asp?id=264
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛