javascript中一些数据类型以及奇怪的特性。具体情况直接运行就知道了!
输出结果:
[img]http://dl2.iteye.com/upload/attachment/0085/5422/246082de-8274-3f49-bd83-55710f592687.png[/img]
[img]http://dl2.iteye.com/upload/attachment/0085/5424/20897927-b652-3938-a88f-5ad6aa3402b5.png[/img]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Data Types And Definitions</title>
<style type="text/css"></style>
<script type="text/javascript">
/*
*/
function factorial(x) {
if (x < 0) throw new Error("factorial: x must be >= 0");
if (x <= 1) {
return 1;
} else {
return x * factorial(x-1);
}
}
//factorial(-2);
var fa = factorial(2);
console.log(" fa = " + fa );
var factorial = function(x) {
if (x < 2) {
return 1;
} else {
return x * arguments.callee(x-1);
}
}
var y = factorial(5); // Returns 120
console.log(" y = " + y );
function myFunc(arg1,arg2){
console.log(" myFunc.length = " + myFunc.length);
console.log(" arguments.length = " + arguments.length);
console.log(" arguments.callee.length = " + arguments.callee.length);
}
myFunc();
console.log(" myFunc.length = " + myFunc.length);//显示函数形参的个数 结果为2
var isArray = function(obj) {
return Object.prototype.toString.apply(obj) === '[object Array]';
}
function myfunc2(){
var s="";
//arguments.callee指的就是函数自己 注意 callee是 arguments的属性,不是函数的的
console.log(" isArray(arguments) = " + isArray(arguments) ); // false
console.log(" apply(arguments) = " + Object.prototype.toString.apply(arguments) ); // [object Arguments]
console.log(" typeof arguments = " + arguments); // [object Arguments]
console.log(" arguments.callee = " + arguments.callee);
console.log(" typeof arguments.callee = " + typeof arguments.callee);//function
var len=arguments.length;
for (var i=0;i<len;i++){
s+=arguments[i];//此处要注意 arguments并非真正的数组,如果需要变为数组,下面有方式方法。
var args=Array.prototype.slice.call(arguments);//将arguments转变成真正的数组,并赋予args
}
console.log(s + " args = " + args);
}
myfunc2(4,4,"s","444");
var obj = new Object();
function a(){}
console.log(typeof (typeof a)); //alerts 'string'
console.log(" typeof a = " + typeof a); //alerts 'function'
console.log(" typeof obj = " + typeof obj); //alerts 'object'
console.log(" typeof null = " + typeof null); //alerts 'object'
console.log(" typeof false = " + typeof false); //alerts 'object'
console.log(" typeof new Array() = " + typeof new Array()); //alerts 'object'
console.log(" new Array() = " + (new Array()) ); //alerts 空白
console.log(" []= " + ( [] ) ); //alerts 空白
console.log(" null instanceof Object = " + null instanceof Object); //evaluates false(null instanceof Object); //evaluates false
console.log(" false instanceof Boolean = " + false instanceof Boolean); // false
console.log(" new Array() == false => " + (new Array() == false) ); //evaluates true
console.log(" [] == false => " + ([] == false) ); //evaluates true
var emptyArray = []; //empty array
console.log(" emptyArray == false => " + ( emptyArray == false) ); //evaluates true
if (emptyArray) {
console.log(" if(emptyArray) ==> true "); //ture
} else {
console.log(" if(emptyArray) ==> false ");
}
var zero = 0;
console.log(" 0 == false => " + ( zero == false) ); //evaluates true – zero is a falsy
console.log(" 0 === false => " + ( zero === false) ); //evaluates false – zero is a number, not a boolean
var someVar = 'hello';
setTimeout(function() { alert(someVar); }, 1000);//out put goodbye
var someVar = 'goodbye';
var someVar = 'hello';
setTimeout((function(someVar) {
return function() { alert(someVar); }//out put hello 参数早就传进去了
})(someVar), 1000);
someVar = 'goodbye';
//http://coding.smashingmagazine.com/2011/05/30/10-oddities-and-secrets-about-javascript/
</script>
</head>
<body>
</body>
</html>
输出结果:
[img]http://dl2.iteye.com/upload/attachment/0085/5422/246082de-8274-3f49-bd83-55710f592687.png[/img]
[img]http://dl2.iteye.com/upload/attachment/0085/5424/20897927-b652-3938-a88f-5ad6aa3402b5.png[/img]