<!DOCTYPE html> <html> <head> <title>Javascript高级程序设计-第一阶段</title> </head> <body> <script type="text/javascript"> /* //1、获取逻辑像素 var pageWidth = window.innerWidth; var pageHeight = window.innerHeight; console.log("pageWidth:" + pageWidth); console.log("pageHeight:" + pageHeight); if (typeof pageWidth != "number") { if (document.compaMode === "CSS1Compat") { } } //2、获取窗口位置 var leftPos = (typeof window.screenLeft == "number") ? window.screenLeft : window.screenX; var topPos = (typeof window.screenTop == "number") ? window.screenTop : window.screenY; console.log("position left:" + leftPos); console.log("position top:" + topPos); //2、 两个比较运算符的却别 console.log("===:" + (null === undefined)); //true console.log("==:" + (null == undefined)); // false 3、 setTimeout() 应用 var date1 = new Date(); console.log(parseInt(date1.getTime())); var date2 = new Date(); setTimeout("", 2000); console.log(parseInt(date2.getTime())); console.log(date1.getTime() - date2.getTime()); 4、 clearTimeout(func, time) 应用 function demo() { var date = new Date(); console.log(date); } var t = setInterval(demo, 500); setTimeout("clearInterval(t)", 3000); 5、 定时器: 所有dom元素渲染完后才开始计时 setTimeout("alert('deefe')", 3000); //5、闭包 function createFunction() { var result; for (var i = 0; i < 10; i++) { result[i] = function() { result[i].index = i; return i; } } console.log(result[0].index); return result; } function createFunction() { var result = new Array(); for (var i = 0; i < 10; i++) { result[i] = function(num) { return function() { result[i].index = i; console.log(i); return num; }; }(i); } return result; } console.log(createFunction()); //6、三目运算符 console.log((x > y ? x : y) > z ? (x > y ? x : y) : z); //7、求最大值 Math.max() 不能传入数组, 只能传入参数列 console.log(max(12, 23, 1)); function max() { var arr = [12112, 21, 212, 12, 3213]; return Math.max(arr); } //8、回调函数,返回上一步操作,每一次结果保存在内存中,占用内存 function factorial1(num) { if (num > 0) { return num * factorial1(num - 1); } else { return 1; } } //9、回调函数,讲结果返回,不占用多余的内存 function factorial2(num, total) { if (num > 0) { return total = num * factorial2(num - 1); } else { return 1; } } var n = 10; console.log("factorial1(" + n + "):" + factorial1(n)); console.log("factorial2(" + n + "):" + factorial2(n)); //10、函数中断方式 function say() { console.log("1"); // 1 return 0; console.log("2"); //函数已经结束没有执行 } 中断循环的方式 1. break 2. continue //11、ECMScript 完整的解析器 alert(eval("1+2")); //12、闰年判断 var j = 1; for (var i = 1; i <= 3000; i++) { if (getYear(i)) { console.log(j + ":" + i); j++; } } function getYear(year) { if (year % 400 == 0 || (year % 4 === 0 && year % 100 != 0)) { return 1; } else { return 0; } } //13、push(),pop(),shift(),unshift()区别 var arr = [1, 2, 3]; arr.push(4); console.log("push(4):" + arr); //1,2,3,4 从尾部添加 var arr = [1, 2, 3]; arr.pop(4); console.log(arr); //1,2 从尾部出栈 var arr = [1, 2, 3]; arr.unshift(4) console.log(arr); //4,1,2,3 从头部添加 var arr = [1, 2, 3]; arr.shift(4); console.log(arr); // 2,3 从尾部添加 //14、++在前面和在后面的区别 var n = 100; alert(++n); // 101 var m = 100; alert(m++); //100 //15、条件操作符 console.log(12 > 4 ? "yes" : "no"); */ </script> </body> </html>