js 面试的坑(三)

╰半夏微凉° 2022-09-25 15:18 219阅读 0赞

判断页面滚动方向(上下)

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. body{
  8. height:1000px;
  9. }
  10. </style>
  11. <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
  12. </head>
  13. <body>
  14. <script type="text/javascript">
  15. $(function() {
  16. function scroll(fn) {
  17. var beforeScrollTop = document.body.scrollTop,
  18. fn = fn || function() {
  19. };
  20. window.addEventListener("scroll", function() {
  21. var afterScrollTop = document.body.scrollTop,
  22. delta = afterScrollTop - beforeScrollTop;
  23. if(delta === 0) return false;
  24. fn(delta > 0 ? "down" : "up");
  25. beforeScrollTop = afterScrollTop;
  26. }, false);
  27. }
  28. scroll(function(direction) {
  29. if(direction == "down") {
  30. console.log("向下滚");
  31. } else {
  32. console.log("向上滚");
  33. }
  34. });
  35. });
  36. </script>
  37. </body>
  38. </html>

深入理解javascript 回调指针

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Document</title>
  6. <script type="text/javascript">
  7. window.onload = function() {
  8. var i=0;
  9. function test() {
  10. document.getElementById('main').innerHTML=i++;
  11. }
  12. setInterval(test, 1000);//注意此处是test,不是test();
  13. }
  14. </script>
  15. </head>
  16. <body>
  17. <p id="main"></p>
  18. </body>
  19. </html>

去掉字符串中所有空格

这里写图片描述

  1. 1 去掉字符串前后所有空格:
  2. 代码如下:
  3. function Trim(str)
  4. {
  5. return str.replace(/(^\s*)|(\s*$)/g, "");
  6. }
  7. 说明:
  8. 如果使用jQuery直接使用$.trim(str)方法即可,str表示要去掉前后所有空格的字符串。
  9. 2 去掉字符串中所有空格(包括中间空格,需要设置第2个参数为:g)
  10. 代码如下:
  11. function Trim(str,is_global)
  12. {
  13. var result;
  14. result = str.replace(/(^\s+)|(\s+$)/g,"");
  15. if(is_global.toLowerCase()=="g")
  16. {
  17. result = result.replace(/\s/g,"");
  18. }
  19. return result;
  20. }

JS获取div距离浏览器窗口的高度(重要)

这里写图片描述
这里写图片描述

制作图片懒加载:http://www.cnblogs.com/libin-1/p/5851872.html

http://www.cnblogs.com/vajoy/p/4263291.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
  7. <style type="text/css">
  8. * {
  9. padding: 0px;
  10. margin: 0px;
  11. }
  12. #main {
  13. height: 2000px;
  14. background: red;
  15. }
  16. #java {
  17. height: 300px;
  18. background: blue;
  19. }
  20. #js{
  21. height: 300px;
  22. background: green;
  23. }
  24. #haha{
  25. height: 300px;
  26. background: yellow;
  27. }
  28. </style>
  29. <script type="text/javascript">
  30. $(function() {
  31. console.log('$("#java").offset().top : '+$("#java").offset().top);
  32. $("#java").offset({
  33. top: 200
  34. });
  35. console.log('document.getElementById("js").getBoundingClientRect().top : '+document.getElementById('js').getBoundingClientRect().top);
  36. console.log('document.getElementById("js").getBoundingClientRect().bottom : '+document.getElementById('js').getBoundingClientRect().bottom);
  37. })
  38. </script>
  39. </head>
  40. <body>
  41. <div id="main">
  42. </div>
  43. <div id="java">
  44. </div>
  45. <div id="js">
  46. </div>
  47. <div id="haha">
  48. </div>
  49. </body>
  50. </html>

点击div外面该div消失

http://www.cnblogs.com/libin-1/p/5746167.html

自定义a标签悬浮title样式

这里写图片描述

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Document</title>
  6. <style>
  7. #tooltip {
  8. border: 1px solid red;
  9. background: #FF6;
  10. position: absolute;
  11. padding: 1px;
  12. color: #333;
  13. display: none;
  14. }
  15. </style>
  16. <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
  17. </head>
  18. <body>
  19. <div id="link">
  20. <p>
  21. <a href="#" class="tooltip" title="这是我的超链接提示1">提示1</a>
  22. </p>
  23. <p>
  24. <a href="#" class="tooltip" title="这是我的超链接提示2">提示2</a>
  25. </p>
  26. <p>
  27. <a href="#" title="这是我的超链接提示1">自带提示1</a>
  28. </p>
  29. <p>
  30. <a href="#" title="这是我的超链接提示2">自带提示2</a>
  31. </p>
  32. </div>
  33. <script type="text/javascript">
  34. $(function() {
  35. var x = 10;
  36. var y = 20;
  37. $("a.tooltip").mouseover(function(e) {
  38. this.myTitle = this.title;
  39. this.title = "";
  40. var tooltip = "<div id='tooltip'>" + this.myTitle + "</div>"; //创建DIV元素
  41. $("#link").append(tooltip); //追加到文档中
  42. $("#tooltip").css({
  43. "top": (e.pageY + y) + "px",
  44. "left": (e.pageX + x) + "px"
  45. }).show(); //设置X Y坐标, 并且显示
  46. }).mouseout(function() {
  47. this.title = this.myTitle;
  48. $("#tooltip").remove(); //移除
  49. }).mousemove(function(e) {
  50. $("#tooltip").css({
  51. "top": (e.pageY + y) + "px",
  52. "left": (e.pageX + x) + "px"
  53. });
  54. })
  55. })
  56. </script>
  57. </body>
  58. </html>

回调函数是什么鬼?

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <script type="text/javascript">
  7. function test(a, b, c) {
  8. setInterval(function() {
  9. b(a);
  10. }, c)
  11. }
  12. test(10, function(i) {
  13. console.log(i);
  14. }, 2000);
  15. </script>
  16. </head>
  17. <body>
  18. </body>
  19. </html>

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript"> function aa(a, b) { return a+b; } function bb(a, b) { return a-b; } function main(a, b) { var i = 10; var j = 5; return(a(i, j) + b(i, j)); } alert(main(aa, bb)); //20 </script> </head> <body> </body> </html>

js如何实现继承

http://blog.csdn.net/james521314/article/details/8645815

JS原型链简单图解

http://www.cnblogs.com/libin-1/p/5820550.html

http://blog.csdn.net/libin_1/article/details/52366524

http://www.cnblogs.com/myqianlan/p/4421950.html

http://www.cnblogs.com/shuiyi/p/5343399.html

http://www.cnblogs.com/shuiyi/p/5305435.html

this,都快忘了你了

这里写图片描述
参考:http://blog.csdn.net/libin_1/article/details/52337576
http://www.cnblogs.com/libin-1/p/5814792.html
http://www.cnblogs.com/beyond-succeed/p/5808290.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <script type="text/javascript">
  7. var name = "window1",
  8. lzh = {
  9. name: "lzh",
  10. sayName: function() {
  11. function innerFunction() {
  12. console.log(_this.name);
  13. }
  14. var _this=this;
  15. innerFunction();
  16. return function() {
  17. console.log(_this.name);
  18. }
  19. }
  20. }
  21. lzh.sayName()();
  22. </script>
  23. <script type="text/javascript">
  24. var name1 = "window2",
  25. lzh1 = {
  26. name: "lzh",
  27. sayName: function() {
  28. function innerFunction() {
  29. console.log(this.name);
  30. }
  31. innerFunction();
  32. return function() {
  33. console.log(this.name);
  34. }
  35. }
  36. }
  37. lzh1.sayName()();
  38. </script>
  39. </head>
  40. <body>
  41. </body>
  42. </html>

prototype,都快忘了你了

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title></title>
  6. <script type="text/javascript">
  7. function LIBIN(a, b) {
  8. this.a = a;
  9. this.b = b;
  10. this.fun = function() {
  11. alert('真是日了dog了');
  12. sb();
  13. }
  14. sb = function() {
  15. alert("卧槽!");
  16. }
  17. }
  18. LIBIN.prototype = {
  19. dianzan: function() {
  20. alert("点赞");
  21. },
  22. budianzan: function() {
  23. alert("拒绝点赞");
  24. }
  25. }
  26. var libin = new LIBIN('小明', 18);
  27. libin.dianzan();
  28. alert(libin.a + ":" + libin.b);
  29. libin.fun();
  30. </script>
  31. </head>
  32. <body>
  33. </body>
  34. </html>
  35. <!DOCTYPE html>
  36. <html>
  37. <head>
  38. <meta charset="utf-8" />
  39. <title></title>
  40. <script type="text/javascript">
  41. function LIBIN(a, b) {
  42. this.a = a;
  43. this.b = b;
  44. this.fun = function() {
  45. alert('真是日了dog了');
  46. sb();
  47. }
  48. sb = function() {
  49. alert("卧槽!");
  50. }
  51. };
  52. LIBIN.prototype = {
  53. sb:"是你",
  54. dianzan: function() {
  55. alert("点赞");
  56. },
  57. budianzan: function() {
  58. alert("拒绝点赞");
  59. }
  60. };
  61. LIBIN.prototype.x="zhangsan";
  62. LIBIN.prototype.saycao=function(){
  63. alert("卧槽");
  64. }
  65. var libin = new LIBIN('小明', 18);
  66. libin.dianzan();
  67. alert(libin.a + ":" + libin.b+":"+libin.x+":"+libin.sb);
  68. libin.fun();
  69. libin.saycao();
  70. </script>
  71. </head>
  72. <body>
  73. </body>
  74. </html>

JavaScript和jQuery的类型判断

参考:http://www.cnblogs.com/likeFlyingFish/p/5840496.html

  1. 对于类型的判断,JavaScripttypeof来进行。
  2. 栗子:
  3. 复制代码
  4. console.log(typeof null); //object
  5. console.log(typeof []); //object
  6. console.log(typeof {}); //object
  7. console.log(typeof new Date()); //object
  8. console.log(typeof new Object); //object
  9. console.log(typeof function(){
  10. }); //function
  11. console.log(typeof alert); //function
  12. console.log(typeof 1); //number
  13. console.log(typeof "abc"); //string
  14. console.log(typeof true); //boolean
  15. 复制代码
  16. 可以看到,typeof并不能够准确的判断出每一种数据类型,比如null和数组等都是object类型。因此,JavaScript判断数据类型不推荐使用typeof
  17. 那么要如何具体判断呢??看一下语法<( ̄3 ̄)> !
  18. {}.toString.call(obj);
  19. 栗子:
  20. 复制代码
  21. console.log({}.toString.call(null)); //[object Null]
  22. console.log({}.toString.call([])); //[object Array]
  23. console.log({}.toString.call({})); //[object Object]
  24. console.log({}.toString.call(new Date())); //[object Date]
  25. console.log({}.toString.call(function(){
  26. })); //[object Function]
  27. console.log({}.toString.call(new Object)); //[object Object]
  28. console.log({}.toString.call(alert)); //[object Function]
  29. console.log({}.toString.call(1)); //[object Number]
  30. console.log({}.toString.call('abc')); //[object String]
  31. console.log({}.toString.call(true)); //[object Boolean]
  32. 复制代码
  33. 哈哈,是不是一目了然呀!!
  34. 那如果你用的是jQuery,就不用这么麻烦喽,可以直接用工具方法$.type(),进行判断
  35. 栗子:
  36. console.log($.type(null)); //null
  37. console.log($.type([])); //array
  38. console.log($.type({})); //object
  39. console.log($.type(1)); //number
  40. ......不全写完了,结果和{}.toString.call(obj);是一样的
  41. 实际上{}.toString.call(obj);就是jQuery$.type()这个工具方法的实现最重要的一段代码(⊙o⊙)哦,神奇吧!赶快去jQuery源码中找找看吧~~
  42. 如果哪里写的有问题,欢迎各路大神指正!

这里写图片描述

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style type="text/css">
  7. div {
  8. width: 200px;
  9. height: 200px;
  10. border: 3px solid red;
  11. padding: 17px;
  12. margin: 16px;
  13. color: blue;
  14. }
  15. </style>
  16. <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
  17. <script type="text/javascript">
  18. $(function() {
  19. console.log("width:" + $("div").width());
  20. console.log("width+padding:" + $("div").innerWidth());
  21. console.log("width+padding+border:" + $("div").outerWidth());
  22. console.log("width+padding+border+margin:" + $("div").outerWidth(true));
  23. })
  24. </script>
  25. </head>
  26. <body>
  27. <div>
  28. <p>width: 200px;</p>
  29. <p> height: 200px;</p>
  30. <p> border: 3px solid red;</p>
  31. <p> padding: 17px;</p>
  32. <p> margin: 16px;</p>
  33. </div>
  34. </body>
  35. </html>

像素转为整数

这里写图片描述

禁止用户输入非数字

  1. <input type="text" placeholder="只能输入数字" onkeyup="this.value = this.value.replace(/\D/,'')" >

window.onload的jQuery写法

  1. window.onload=function(){
  2. } === 或者$(window).load(function(){
  3. });

js判断终端是PC还是移动端

  1. function IsPC() {
  2. var userAgentInfo = navigator.userAgent;
  3. var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");
  4. var flag = true;
  5. for (var v = 0; v < Agents.length; v++) {
  6. if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }
  7. }
  8. return flag; }

最简单的排序

  1. <script type="text/javascript">
  2. var a = [1, 18, 23, 9, 16, 10, 29, 17];
  3. var t = 0;
  4. for(var i = 0; i < a.length; i++) {
  5. for(var j = i + 1; j < a.length; j++) {
  6. if(a[i] > a[j]) {
  7. t = a[i];
  8. a[i] = a[j];
  9. a[j] = t;
  10. }
  11. }
  12. }
  13. console.log(a); //[1, 9, 10, 16, 17, 18, 23, 29]
  14. </script>

判断终端类型跳转

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Document</title>
  6. <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
  7. <script type="text/javascript">
  8. if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {  
  9. window.location.href = "http://www.baidu.com";
  10. } else {
  11. window.location.href = "https://wx.qq.com/";
  12. }
  13. </script>
  14. </head>
  15. <body>
  16. </body>
  17. </html>

深入理解JavaScript中 fn() 和 return fn() 的区别

在js中,经常会遇到在函数里调用其它函数的情况,这时候会有 fn() 这种调用方式,还有一种是 return fn() 这种调用方式,一些初学者经常会一脸萌逼地被这两种方式给绕晕了。这里用一个优雅的面试题来分析一下两种方式的不同之处。

  1. var i = 0;
  2. function fn(){
  3. i++;
  4. if(i < 10){
  5. fn();
  6. }else{
  7. return i;
  8. }
  9. }
  10. var result = fn();
  11. console.log(result);

这是一道隐藏了坑的面试题,看似很简单,大部分人可能想都不想就答出了10。而实际上运行可知打印出来的是 undefined。这道陷阱题很直观的体现出了前面所说的问题,当我们将执行fn的那一行修改为:

  1. var i = 0;
  2. function fn(){
  3. i++;
  4. if(i < 10){
  5. return fn();
  6. }else{
  7. return i;
  8. }
  9. }
  10. var result = fn();
  11. console.log(result);

这时,会发现打印出来的结果终于不负众望的是 10 了。

为什么这里加不加return区别会这么大?

这里的主要原因很简单,JavaScript的函数都是有默认返回值的,如果函数结尾不写return,会默认返回undefined,这就是为什么在chrome的console控制台里,写代码经常下面会出现一行undefined的原因。

再仔细看看这个例子,当i自增到9的时候,也就是倒数第二次递归调用fn的那一次,如果没有return,这一次执行完fn,会默认return undefined,而不会继续下一次递归了。当加上了 return,在这里则会继续最后一次递归,即i=10的时候,跳入else里面返回得到正确的10。

说到这里,可以引申出一个更为经典的例子,著名的二分查

  1. var midMath.floor((arr.length - 1) / 2);
  2. function search(n, mid) {
  3. if (n > arr[mid]) {
  4. mid = Math.floor((mid + arr.length) / 2);
  5. return search(n, mid);
  6. } else if (n < arr[mid]) {
  7. mid = Math.floor((mid - 1) / 2);
  8. return search(n, mid);
  9. } else {
  10. return mid;
  11. }
  12. }
  13. var index = search(n, mid);
  14. console.log(index);

也是需要多次递归调用,很多新手在第一次实现这个算法的时候经常会犯的一个错误就是忘记在递归的函数前加上return,最后导致返回结果是undefined,这里的道理也和前面是类似的,不加return,会导致递归后,直接返回undefined,不会继续下一次递归。

localStorage使用很简单

这里写图片描述
这里写图片描述

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Document</title>
  6. <script type="text/javascript">
  7. window.onload = function() {
  8. window.localStorage.clear(); //清除所有的变量和值
  9. window.localStorage.a = 100;
  10. window.localStorage["b"] = 200;
  11. window.localStorage.setItem("c", 300);;
  12. console.log("window.localStorage.a=" + window.localStorage.a);
  13. console.log("window.localStorage['b']=" + window.localStorage["b"]);
  14. console.log("window.localStorage.getItem('c')=" + window.localStorage.getItem("c"));
  15. console.log(window.localStorage);
  16. console.log(typeof window.localStorage);
  17. for (var i = 0; i < window.localStorage.length; i++) {
  18. var key1 = window.localStorage.key(i);
  19. var key2 = window.localStorage.getItem(key1);
  20. console.log("字段名:" + key1 + " 值:" + key2);
  21. }
  22. console.log(typeof window.localStorage.a);
  23. console.log(typeof window.localStorage['b']);
  24. console.log(typeof window.localStorage.getItem('c'));
  25. console.log("空了:" + window.localStorage);
  26. window.localStorage.SB = "我是你大爷!";
  27. window.localStorage.aaa = "this is test!";
  28. window.localStorage.removeItem("aaa");
  29. console.log("-------------------------------------------------");
  30. var storage = window.localStorage;
  31. var data = {
  32. name: 'xiecanyong',
  33. sex: 'man',
  34. hobby: 'program'
  35. };
  36. var d = JSON.stringify(data);
  37. console.log(typeof d);
  38. storage.setItem("data", d);
  39. console.log(storage.data);
  40. var json = storage.getItem("data");
  41. var jsonObj = JSON.parse(json);
  42. console.log(typeof jsonObj);
  43. console.log(storage.data);
  44. }
  45. </script>
  46. </head>
  47. <body>
  48. </body>
  49. </html>

注意

这里写图片描述

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
  7. <script type="text/javascript">
  8. "use strict";
  9. var B = {
  10. "name": "SBSBSBS",
  11. "age": "32",
  12. say: function() {
  13. console.log("不加this可能会报错:"+this.name+":"+this.age);
  14. }
  15. }
  16. B.say();
  17. var name="我是你大爷";
  18. function USR(name) {
  19. this.name = name;
  20. this.say = function() {
  21. console.log("window下的: " + window.name);
  22. console.log("USR 里面的:" + this.name);
  23. }
  24. };
  25. var a = new USR("我是谁?");
  26. a.say();
  27. </script>
  28. <style type="text/css">
  29. * {
  30. margin: 0px;
  31. padding: 0px;
  32. }
  33. div {
  34. width: 50%;
  35. height: 200px;
  36. }
  37. .demo {
  38. float: left;
  39. background: red;
  40. }
  41. .main {
  42. float: right;
  43. background: green;
  44. }
  45. </style>
  46. </head>
  47. <div class="demo">
  48. </div>
  49. <div class="main">
  50. </div>
  51. <body>
  52. </body>
  53. </html>

深入理解变量提升和函数提升

这里写图片描述

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Document</title>
  6. <script type="text/javascript">
  7. console.log(x());
  8. console.log(x);
  9. var x = 10;
  10. console.log(x);
  11. x = 20;
  12. function x() {
  13. var a = new Array();
  14. }
  15. console.log(x);
  16. if (true) {
  17. var a = 1;
  18. } else {
  19. var b = true;
  20. }
  21. console.log(a);
  22. console.log(b);
  23. </script>
  24. </head>
  25. <body>
  26. </body>
  27. </html>

constructor 和 instanceof

这里写图片描述

  1. instanceof 用于判断一个变量是否某个对象的实例,或用于判断一个变量是否某个对象的实例;
  2. constructor 用于判断一个变量的原型,constructor 属性返回对创建此对象的数组函数的引用。
  3. Javascript中对象的prototype属性的解释是:返回对象类型原型的引用。
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <meta charset="utf-8" />
  8. <title></title>
  9. <script type="text/javascript">
  10. console.log("----------------Number---------------");
  11. var A = 123;
  12. console.log(A instanceof Number); //false
  13. console.log(A.constructor == Number); //true
  14. console.log(A.constructor);
  15. console.log("----------------String---------------");
  16. var B = "javascript";
  17. console.log(B instanceof String); //false
  18. console.log(B.constructor == String); //true
  19. console.log(B.constructor);
  20. console.log("----------------Boolean---------------");
  21. var C = true;
  22. console.log(C instanceof Boolean); //false
  23. console.log(C.constructor == Boolean); //true
  24. console.log(C.constructor);
  25. console.log("----------------null---------------");
  26. var D = null;
  27. console.log(D instanceof Object); //false
  28. //console.log(D.constructor == null); //报错
  29. //console.log(D.constructor); //报错
  30. console.log("----------------undefined---------------");
  31. var E = undefined;
  32. //console.log(E instanceof undefined); // //报错
  33. //console.log(E.constructor == undefined); //报错
  34. //console.log(E.constructor); //报错
  35. console.log("----------------function---------------");
  36. var F = function() {
  37. };
  38. console.log(F instanceof Function);
  39. console.log(F.constructor == Function);
  40. console.log(F.constructor);
  41. console.log("----------------new function---------------");
  42. function SB() {
  43. };
  44. var G = new SB();
  45. console.log(G instanceof SB);
  46. console.log(G.constructor == SB);
  47. console.log(G.constructor);
  48. console.log("----------------new Object---------------");
  49. var H = new Object;
  50. console.log(H instanceof Object);
  51. console.log(H.constructor == Object);
  52. console.log(H.constructor);
  53. console.log("-----------------Array--------------");
  54. var I = [];
  55. console.log(I instanceof Array);
  56. console.log(I.constructor == Array);
  57. console.log(I.constructor);
  58. console.log("-----------------JSON--------------");
  59. var J = {
  60. "sb": "javascript",
  61. "node": "very SB"
  62. };
  63. console.log(J instanceof Object);
  64. console.log(J.constructor == Object);
  65. console.log(J.constructor);
  66. </script>
  67. </head>
  68. <body>
  69. </body>
  70. </html>
  71. <!DOCTYPE html>
  72. <html>
  73. <head>
  74. <meta charset="utf-8" />
  75. <title></title>
  76. <script type="text/javascript">
  77. var A = new Array();
  78. var B = new Boolean();
  79. console.log(A instanceof Array); // true
  80. console.log(A instanceof Boolean); // false
  81. console.log(B.constructor == Array); // false
  82. console.log(B.constructor == Boolean); // true
  83. </script>
  84. </head>
  85. <body>
  86. </body>
  87. </html>
  88. <!DOCTYPE html>
  89. <html>
  90. <head>
  91. <meta charset="utf-8" />
  92. <title></title>
  93. <script type="text/javascript">
  94. function sb(name, age) {
  95. this.name = name;
  96. this.age = age;
  97. this.sbs = function() {
  98. console.log(this.name + "," + this.age);
  99. }
  100. }
  101. var A = new Array();
  102. var B = new Boolean();
  103. var C=new sb("libin",28);
  104. document.writeln(A.constructor + "<br />"); //function Array() { [native code] }
  105. document.writeln(B.constructor + "<br />"); //function Boolean() { [native code] }
  106. document.writeln(C.constructor + "<br />"); //function sb(name, age) { this.name = name; this.age = age; this.sbs = function() { console.log(this.name + "," + this.age); } }
  107. C.name="李斌";
  108. C.sbs(); //李斌,28
  109. </script>
  110. </head>
  111. <body>
  112. </body>
  113. </html>

注意

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <script type="text/javascript">
  7. function aaa(){
  8. alert(a); //报错:a is not defined
  9. }
  10. function bbb(){
  11. var a=10;
  12. aaa();
  13. }
  14. bbb();
  15. </script>
  16. </head>
  17. <body>
  18. </body>
  19. </html>
  20. <!DOCTYPE html>
  21. <html>
  22. <head>
  23. <meta charset="UTF-8">
  24. <title></title>
  25. <script type="text/javascript">
  26. var a=100;
  27. function aaa(){
  28. alert(a); //100
  29. }
  30. function bbb(){
  31. var a=10;
  32. aaa();
  33. }
  34. bbb();
  35. </script>
  36. </head>
  37. <body>
  38. </body>
  39. </html>

数组去重

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Document</title>
  6. <script type="text/javascript">
  7. var a = [1, 2, 5, 5, 7, 9];
  8. Array.prototype.unique = function() {
  9. var b = [];
  10. for (var i = 0; i < this.length; i++) {
  11. if (b.indexOf(this[i]) == -1) {
  12. b.push(this[i]);
  13. }
  14. }
  15. return b;
  16. }
  17. document.writeln(a.unique());
  18. </script>
  19. </head>
  20. <body>
  21. </body>
  22. </html>

可恶的forEach

这里写图片描述

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Document</title>
  6. <script type="text/javascript">
  7. var a = ["a", "b", "c", "d", "e"];
  8. a.forEach(function(i, j) {
  9. //注意参数i,j对应关系相反
  10. console.log(i + ":" + j); //妈的个巴子,j居然是索引
  11. })
  12. </script>
  13. </head>
  14. <body>
  15. </body>
  16. </html>

call 和 apply 是为了动态改变 this 而出现的

这里写图片描述

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Document</title>
  6. <script type="text/javascript">
  7. //apply 、 call 、bind 三者都是用来改变函数的this对象的指向的;
  8. //apply 、 call 、bind 三者第一个参数都是this要指向的对象,也就是想指定的上下文;
  9. //apply 、 call 、bind 三者都可以利用后续参数传参;
  10. //bind 是返回对应函数,便于稍后调用;apply 、call 则是立即调用 。
  11. var xw = {
  12. name: "小王",
  13. gender: "男",
  14. age: 24,
  15. say: function(school, grade) {
  16. console.log(this.name + " , " + this.gender + " ,今年" + this.age + " ,在" + school + "上" + grade);
  17. }
  18. }
  19. var xh = {
  20. name: "小红",
  21. gender: "女",
  22. age: 18
  23. }
  24. xw.say("清华大学","本科");
  25. xw.say.call(xh,"北京大学","研究僧"); //call后面的参数与say方法中是一一对应的
  26. xw.say.apply(xh,["新东方","电气焊"]); //而apply的第二个参数是一个数组,数组中的元素是和say方法中一一对应的
  27. xw.say.bind(xh)("蓝翔技校","挖掘机"); //bind返回的仍然是一个函数,所以我们还可以在调用的时候再进行传参
  28. </script>
  29. </head>
  30. <body>
  31. </body>
  32. </html>

js计算div 宽高等(面试常问)

这里写图片描述

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Document</title>
  6. <style type="text/css">
  7. * {
  8. padding: 0px;
  9. margin: 0px;
  10. }
  11. #main {
  12. width: 200px;
  13. height: 300px;
  14. padding: 3px;
  15. margin: 7px;
  16. background: green;
  17. border: 5px solid red;
  18. position: absolute;
  19. top: 100px;
  20. left: 200px;
  21. }
  22. span{
  23. color: red;
  24. }
  25. </style>
  26. <script type="text/javascript">
  27. function G(a) {
  28. return document.getElementById(a);
  29. }
  30. window.onload = function() {
  31. document.onmousemove = function(e) {
  32. document.getElementsByTagName('span')[0].innerText = e.clientX + "," + e.clientY;
  33. };
  34. G("main").onmousemove = function(e) {
  35. document.getElementsByTagName('span')[1].innerText = e.offsetX + "," + e.offsetY;
  36. }
  37. console.log("屏幕宽高:" + window.screen.width+","+window.screen.height);
  38. console.log("body宽高:" +document.body.clientWidth +","+ document.body.clientHeight);
  39. console.log("offsetWidth包含width,padding,border:" + G('main').offsetWidth);
  40. console.log("offsetHeight包含height,padding,border:" + G('main').offsetHeight);
  41. console.log("offsetTop包含top,margin:" + G('main').offsetTop);
  42. console.log("offsetLeft包含left,margin:" + G('main').offsetLeft);
  43. }
  44. </script>
  45. </head>
  46. <body>
  47. <div id="main">
  48. </div>
  49. <p>相对窗口的座标:e.clientX,e.clientY:(<span>0,0</span>)</p>
  50. <p>相对容器的坐标:e.offsetX,e.offsetY:(<span>0,0</span>)</p>
  51. </body>
  52. </html>

回到顶部回到底部

  1. $("#dwn").click(function() {
  2. $('html,body').animate({
  3. scrollTop: $(document).height()
  4. }, 600)
  5. });
  6. $(".logo2").click(function() {
  7. $('html,body').animate({
  8. scrollTop: 0
  9. }, 600)
  10. });

js数据类型判断和数组判断

  1. var a = [];
  2. console.log(a instanceof Array) //返回true
  3. var b = {};
  4. console.log(b instanceof Object) //返回true
  5. 这么基础的东西实在不应该再记录了,不过嘛,温故知新~就先从数据类型开始吧
  6. js六大数据类型:numberstringobjectBooleannullundefined
  7. string 由单引号或双引号来说明,如"string"
  8. number:什么整数啊浮点数啊都叫数字,你懂的~
  9. Boolean: 就是truefalse
  10. undefined:未定义,就是你创建一个变量后却没给它赋值~
  11. null: 故名思久,null就是没有,什么也不表示
  12. object: 这个我也很难解释的说。就是除了上面五种之外的类型
  13. --------------------上面的都是浮云,下面的才是神马------------------------------
  14. 数据类型判断之 typeof
  15. typeof可以解决大部分的数据类型判断,是一个一元运算,放在一个运算值之前,其返回值为一个字符串,该字符串说明运算数的类型,所以判断某个是否为String类型,可以直接 if(typeof(你的值) == "string"){}
  16. 以下是各种数据类型返回结果:
  17. 复制代码
  18. var a="string"; console.log(a); //string
  19. var a=1; console.log(a); //number
  20. var a=false; console.log(a); //boolean
  21. var a; console.log(typeof a); //undfined
  22. var a = null; console.log(typeof a); //object
  23. var a = document; console.log(typeof a); //object
  24. var a = []; console.log(a); //object
  25. var a = function(){}; console.log(typeof a) //function 除了可以判断数据类型还可以判断function类型
  26. 复制代码
  27. 这样一来就很明显了,除了前四个类型外,null、对象、数组返回的都是object类型;
  28. 对于函数类型返回的则是function,再比如typeof(Date),typeof(eval)等。
  29. 然后这里就可以再引申出另一个灰常热门并且解决方法已普遍存在的问题,如何判断数据是个数组类型?
  30. ---------------------------------------其实这才是我的目的,咩~----------------------------------------------
  31. js判断数组类型的方法
  32. 方法一之 instanceof
  33. instance,故名思义,实例,例子,所以instanceof 用于判断一个变量是否某个对象的实例,是一个三目运算式---和typeof最实质上的区别
  34. a instanceof b?alert("true"):alert("false") //注意b值是你想要判断的那种数据类型,不是一个字符串,比如Array
  35. 举个栗子:
  36. var a=[];
  37. console.log(a instanceof Array) //返回true
  38. 方法二之 constructor
  39. W3C定义中的定义:constructor 属性返回对创建此对象的数组函数的引用
  40. 就是返回对象相对应的构造函数。从定义上来说跟instanceof不太一致,但效果都是一样的
  41. 如: (a instanceof Array) //a是否Array的实例?true or false
  42.   (a.constructor == Array) // a实例所对应的构造函数是否为Array? true or false
  43. 举个栗子:
  44. 复制代码
  45. function employee(name,job,born){
  46. this.name=name;
  47. this.job=job;
  48. this.born=born;
  49. }
  50. var bill=new employee("Bill Gates","Engineer",1985);
  51. console.log(bill.constructor); //输出function employee(name, jobtitle, born){
  52. this.name = name; this.jobtitle = job; this.born = born;}
  53. 复制代码
  54. 那么判断各种类型的方法就是:
  55. console.log([].constructor == Array);
  56. console.log({}.constructor == Object);
  57. console.log("string".constructor == String);
  58. console.log((123).constructor == Number);
  59. console.log(true.constructor == Boolean);
  60. -------------------------------------以下不是原创--------------------------------------
  61. 较为严谨并且通用的方法:
  62. function isArray(object){
  63. return object && typeof object==='object' &&
  64. Array == object.constructor;
  65. }
  66. !!注意:
  67. 使用instaceofconstrucor,被判断的array必须是在当前页面声明的!比如,一个页面(父页面)有一个框架,框架中引用了一个页面(子页面),在子页面中声明了一个array,并将其赋值给父页面的一个变量,这时判断该变量,Array == object.constructor;会返回false
  68. 原因:
  69. 1array属于引用型数据,在传递过程中,仅仅是引用地址的传递。
  70. 2、每个页面的Array原生对象所引用的地址是不一样的,在子页面声明的array,所对应的构造函数,是子页面的Array对象;父页面来进行判断,使用的Array并不等于子页面的Array;切记,不然很难跟踪问题!
  71. 方法三之 特性判断法
  72. 以上方法均有一定的缺陷,但要相信人民大众的智慧是无所不能及的,我们可根据数组的一些特性来判断其类型
  73. 复制代码
  74. function isArray(object){
  75. return object && typeof object==='object' &&
  76. typeof object.length==='number' &&
  77. typeof object.splice==='function' &&
  78. //判断length属性是否是可枚举的 对于数组 将得到false
  79. !(object.propertyIsEnumerable('length'));
  80. }
  81. 复制代码
  82. lengthsplice并不一定是数组,因为可以为对象添加属性,而不能枚举length属性,才是最重要的判断因子。
  83. ps: 在这里普及下 propertyIsEnumerable 方法:
  84. object. propertyIsEnumerable(proName)
  85. 判断指定的属性是否可列举
  86. 备注:如果 proName 存在于 object 中且可以使用一个 ForIn 循环穷举出来,那么 propertyIsEnumerable 属性返回 true。如果 object 不具有所指定的属性或者所指定的属性不是可列举的,那么 propertyIsEnumerable 属性返回 false
  87. propertyIsEnumerable 属性不考虑原型链中的对象。
  88. 示例:
  89. var a = new Array("apple", "banana", "cactus");
  90. document.write(a.propertyIsEnumerable(1));
  91. 方法四之 最简单的方法
  92. 对于这种方法,以下有几个链接可供参考解释:
  93. http://blog.csdn.net/zhangw428/article/details/4171630
  94. http://my.oschina.net/sfm/blog/33197
  95. http://openxtiger.iteye.com/blog/1893378
  96. function isArray(o) {
  97. return Object.prototype.toString.call(o) === ‘[object Array]‘;
  98. }
  99. 当然,你知道啦,这篇东西不完全是出自我手的啦,

全局和局部变量

这里写图片描述

  1. 这里写代码片
  2. <!DOCTYPE HTML>
  3. <html>
  4. <head>
  5. <script type="text/javascript">
  6. //只要函数里不管什么时候只要定义var ,变量就是局部变量
  7. var a = 100;
  8. function test() {
  9. console.log("a=" + a); //100
  10. a = 10;
  11. console.log("a=" + a); //10
  12. }
  13. test();
  14. console.log("a=" + a); //10
  15. console.log("---------------------------------------");
  16. var b = 100;
  17. function test1() {
  18. console.log("b=" + b); //undefined
  19. var b = 10;
  20. console.log("b=" + b); //10
  21. }
  22. test1();
  23. console.log("b=" + b); //100
  24. console.log("---------------------------------------");
  25. var c = 10;
  26. function test2() {
  27. c = 100;
  28. console.log("c=" + c); //100
  29. console.log("this.c=" + this.c); //10
  30. var c; //c 就是局部变量
  31. console.log("c=" + c); //100
  32. }
  33. test2();
  34. console.log("c=" + c); //10
  35. </script>
  36. </head>
  37. <body>
  38. <div id="sse">
  39. </div>
  40. </body>
  41. </html>

反转字符串

  1. <script type="text/javascript">
  2. var a = "hello"
  3. document.writeln(a.split("").reverse().join("")); //olleh
  4. </script>

js倒计时

  1. <!Doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>下班倒计时</title>
  6. <style>
  7. * {
  8. margin: 0;
  9. padding: 0;
  10. }
  11. body {
  12. font-size: 16px;
  13. text-align: center;
  14. font-family: arial;
  15. }
  16. .time {
  17. margin-top: 10px;
  18. border: 1px solid red;
  19. height: 30px;
  20. padding: 2px;
  21. line-height: 30px;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div class="time">
  27. <span id="t_d">00天</span>
  28. <span id="t_h">00时</span>
  29. <span id="t_m">00分</span>
  30. <span id="t_s">00秒</span>
  31. </div>
  32. <script>
  33. setInterval(function() {
  34. var EndTime = new Date('2016/06/13 00:00:00');
  35. var NowTime = new Date();
  36. var t = EndTime.getTime() - NowTime.getTime();
  37. var d = 0;
  38. var h = 0;
  39. var m = 0;
  40. var s = 0;
  41. if (t >= 0) {
  42. d = Math.floor(t / 1000 / 60 / 60 / 24);
  43. h = Math.floor(t / 1000 / 60 / 60 % 24);
  44. m = Math.floor(t / 1000 / 60 % 60);
  45. s = Math.floor(t / 1000 % 60);
  46. }
  47. document.getElementById("t_d").innerHTML = d + "天";
  48. document.getElementById("t_h").innerHTML = h + "时";
  49. document.getElementById("t_m").innerHTML = m + "分";
  50. document.getElementById("t_s").innerHTML = s + "秒";
  51. }, 10);
  52. </script>
  53. </body>
  54. </html>

检测PC端和移动端的方法总结

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
  6. <script type="text/javascript" src="js/jquery-2.1.4.min.js"></script>
  7. <title>Document</title>
  8. <script type="text/javascript">
  9. $(function() {
  10. if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {  
  11. console.log(1);
  12. }
  13. })
  14. </script>
  15. </head>
  16. <body>
  17. <div></div>
  18. </body>
  19. </html>

## 其他办法: ##

  1. 正在苦逼的实习中,昨天公司让做一个页面,涉及到检测终端的问题,如果是手机设备,就跳转到指定的网页上,以前写响应式布局的时候都是用的同一套代码,然后通过css@media screen来实现布局的差异化适应,but现在情景不一样了,所以看了点资料,做个总结
  2. 方法一、还是用@media screen
  3. 思路:css使用媒体查询,当屏幕小于760px时,使某个元素的样式发生改变,然后通过js检测到这个改变,就可以知道现在切换到移动端了
  4. css代码:
  5. 1
  6. 2
  7. 3
  8. 4
  9. /* 检测小屏幕- */
  10. @media only screen and (max-width: 760px) {
  11.   #some-element { display: none; }
  12. }
  13. js代码:
  14. 1
  15. 2
  16. 3
  17. 4
  18. 5
  19. 6
  20. 7
  21. 8
  22. 9
  23. $( document ).ready(function() {
  24.   var isMobile = false;//默认是pc端
  25.   if( $('#some-element').css('display')=='none') {
  26. is_mobile = true;
  27.   }
  28.   if (isMobile == true) {
  29. //对移动端进行处理
  30.   }
  31. });
  32. 方法二、通过navigator.userAgent字符串检测
  33. 思路:Navigator对象包含有关浏览器的信息,通过检测userAgent字符串,然后使用正则表达式进行匹配,我们自然就能知道用户是否在使用移动端的浏览器啦
  34. 先上个简化版的,意思意思下
  35. 1
  36. 2
  37. 3
  38. 4
  39. 5
  40. 6
  41. 7
  42. 8
  43. var isMobile = false;
  44. // 检测userAgent
  45. if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
  46.   isMobile = true;
  47. }
  48. if(isMobile){
  49.   //移动端的处理逻辑
  50. }
  51. 其实还可以用jQuery,but jQuery 1.9 版开始,移除了 $.browser $.browser.version
  52. 1
  53. $.browser.device = (/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(navigator.userAgent.toLowerCase()));
  54. 通过上面那段代码基本就能检测到我们能常用的移动终端了,但是后来我在stackoverflow发现一哥们检测得更加全面牛逼:
  55. 1
  56. 2
  57. 3
  58. 4
  59. 5
  60. 6
  61. 7
  62. 8
  63. var isMobile = false;//默认PC端
  64. // 检测userAgent
  65. if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|ipad|iris|kindle|Android|Silk|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(navigator.userAgent)
  66. || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(navigator.userAgent.substr(0,4)))
  67. {
  68.   isMobile = true;
  69. }
  70. if(isMobile){<br>  //移动端的处理逻辑<br>}
  71. 方法三、通过Window.matchMedia()检测
  72. 思路:Window.matchMedia()用来检查mediaquery语句,扔个MDN的传送门。它返回一个MediaQueryList对象。该对象有两个属性
  73. media:查询语句的内容。
  74. matches:如果查询结果为真,值为true,否则为false
  75. 代码实现如下:
  76. 1
  77. 2
  78. 3
  79. 4
  80. 5
  81. var isMobile = false;//默认PC端
  82. var result = window.matchMedia("<code>only screen and</code> (max-width: 760px)");
  83. if (result.matches){
  84.   isMobile = true;
  85. }
  86. 如果在PC端上使用Window.matchMedia()的话IE10以下是不支持的,但是我们只是用来检测终端哈,IE不支持就算了,移动端上安卓3.0以上都没有问题,so~~
  87. 方法四、检测移动端的TouchEvent事件
  88. 思路:使用document.createEvent()创建TouchEvent事件,如果成功那就是移动端了,返回truepc端是没有TouchEvent事件的,所以会出错,返回false
  89. 代码实现:
  90. 1
  91. 2
  92. 3
  93. 4
  94. 5
  95. 6
  96. 7
  97. 8
  98. 9
  99. 10
  100. 11
  101. var isMobile = false;//默认PC端
  102. function mobile() {
  103. try{
  104. document.createEvent("TouchEvent");
  105. return true;
  106. }
  107. catch(e){
  108. return false;
  109. }
  110. }
  111. isMobile=mobile();
  112. 简洁方便~~
  113. 方法五、使用Device.js
  114. 这个库就没啥好讲的了,自己跟着套代码就OK

arguments是对象,不是数组

这里写图片描述

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <script type="text/javascript" src="js/jquery-2.1.4.min.js"></script>
  6. <title>document</title>
  7. <script type="text/javascript">
  8. function fun1() {
  9. // if (arguments.length == 0) {
  10. // return 0;
  11. // };
  12. // if (arguments.length == 1) {
  13. // return arguments[0];
  14. // };
  15. // if (arguments.length == 2) {
  16. // return arguments[0] + arguments[1];
  17. // }
  18. console.log(typeof arguments);
  19. console.log(arguments);
  20. switch (arguments.length) {
  21. case 0:
  22. return 0;
  23. break;
  24. case 1:
  25. return arguments[0];
  26. break;
  27. case 2:
  28. return arguments[0] + arguments[1];
  29. break;
  30. default:
  31. break;
  32. }
  33. }
  34. console.log(fun1());
  35. console.log(fun1(275));
  36. console.log(fun1(275,25));
  37. </script>
  38. </head>
  39. <body>
  40. </body>
  41. </html>

JavaScript 函数重载

这里写图片描述

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <script type="text/javascript" src="js/jquery-2.1.4.min.js"></script>
  6. <title>document</title>
  7. <script type="text/javascript">
  8. function fun1() {
  9. // if (arguments.length == 0) {
  10. // return 0;
  11. // };
  12. // if (arguments.length == 1) {
  13. // return arguments[0];
  14. // };
  15. // if (arguments.length == 2) {
  16. // return arguments[0] + arguments[1];
  17. // }
  18. switch (arguments.length) {
  19. case 0:
  20. return 0; //arguments是对象,不是数组
  21. break;
  22. case 1:
  23. return arguments[0];
  24. break;
  25. case 2:
  26. return arguments[0] + arguments[1];
  27. break;
  28. default:
  29. break;
  30. }
  31. }
  32. console.log(fun1());
  33. console.log(fun1(275));
  34. console.log(fun1(275,25));
  35. </script>
  36. </head>
  37. <body>
  38. </body>
  39. </html>

apply、call 是什么?

  1. javascript 中,call apply 都是为了改变某个函数运行时的上下文(context)而存在的,换句话说,就是为了改变函数体内部 this 的指向。
  2. JavaScript 的一大特点是,函数存在「定义时上下文」和「运行时上下文」以及「上下文是可以改变的」这样的概念。
  3. 先来一个栗子:
  4. 1
  5. 2
  6. 3
  7. 4
  8. 5
  9. 6
  10. 7
  11. 8
  12. 9
  13. 10
  14. 11
  15. function fruits() {
  16. }
  17. fruits.prototype = {
  18. color: "red",
  19. say: function() {
  20. console.log("My color is " + this.color);
  21. }
  22. }
  23. var apple = new fruits;
  24. apple.say(); //My color is red
  25. 但是如果我们有一个对象banana= {color : "yellow"} ,我们不想对它重新定义 say 方法,那么我们可以通过 call apply apple say 方法:
  26. 1
  27. 2
  28. 3
  29. 4
  30. 5
  31. banana = {
  32. color: "yellow"
  33. }
  34. apple.say.call(banana); //My color is yellow
  35. apple.say.apply(banana); //My color is yellow
  36. 所以,可以看出 call apply 是为了动态改变 this 而出现的,当一个 object 没有某个方法(本栗子中banana没有say方法),但是其他的有(本栗子中applesay方法),我们可以借助callapply用其它对象的方法来操作。
  37. applycall 的区别
  38. 对于 applycall 二者而言,作用完全一样,只是接受参数的方式不太一样。例如,有一个函数定义如下:
  39. 1
  40. 2
  41. 3
  42. var func = function(arg1, arg2) {
  43. };
  44. 就可以通过如下方式来调用:
  45. 1
  46. 2
  47. func.call(this, arg1, arg2);
  48. func.apply(this, [arg1, arg2])
  49. 其中 this 是你想指定的上下文,他可以是任何一个 JavaScript 对象(JavaScript 中一切皆对象),call 需要把参数按顺序传递进去,而 apply 则是把参数放在数组里。  
  50. JavaScript 中,某个函数的参数数量是不固定的,因此要说适用条件的话,当你的参数是明确知道数量时用 call
  51. 而不确定的时候用 apply,然后把参数 push 进数组传递进去。当参数数量不确定时,函数内部也可以通过 arguments 这个数组来遍历所有的参数。
  52. 为了巩固加深记忆,下面列举一些常用用法:
  53. 1、数组之间追加
  54. 1
  55. 2
  56. 3
  57. 4
  58. var array1 = [12 , "foo" , {name "Joe"} , -2458];
  59. var array2 = ["Doe" , 555 , 100];
  60. Array.prototype.push.apply(array1, array2);
  61. /* array1 值为 [12 , "foo" , {name "Joe"} , -2458 , "Doe" , 555 , 100] */
  62. 2、获取数组中的最大值和最小值
  63. 1
  64. 2
  65. 3
  66. var numbers = [5, 458 , 120 , -215 ];
  67. var maxInNumbers = Math.max.apply(Math, numbers), //458
  68. maxInNumbers = Math.max.call(Math,5, 458 , 120 , -215); //458
  69. number 本身没有 max 方法,但是 Math 有,我们就可以借助 call 或者 apply 使用其方法。
  70. 3、验证是否是数组(前提是toString()方法没有被重写过)
  71. 1
  72. 2
  73. 3
  74. functionisArray(obj){
  75. return Object.prototype.toString.call(obj) === '[object Array]' ;
  76. }
  77. 4、类(伪)数组使用数组方法
  78. 1
  79. var domNodes = Array.prototype.slice.call(document.getElementsByTagName("*"));
  80. Javascript中存在一种名为伪数组的对象结构。比较特别的是 arguments 对象,还有像调用 getElementsByTagName , document.childNodes 之类的,它们返回NodeList对象都属于伪数组。不能应用 Array下的 push , pop 等方法。
  81. 但是我们能通过 Array.prototype.slice.call 转换为真正的数组的带有 length 属性的对象,这样 domNodes 就可以应用 Array 下的所有方法了。
  82. 深入理解运用applycall
  83. 下面就借用一道面试题,来更深入的去理解下 apply call
  84. 定义一个 log 方法,让它可以代理 console.log 方法,常见的解决方法是:
  85. 1
  86. 2
  87. 3
  88. 4
  89. 5
  90. function log(msg) {
  91. console.log(msg);
  92. }
  93. log(1); //1
  94. log(1,2); //1
  95. 上面方法可以解决最基本的需求,但是当传入参数的个数是不确定的时候,上面的方法就失效了,这个时候就可以考虑使用 apply 或者 call,注意这里传入多少个参数是不确定的,所以使用apply是最好的,方法如下:
  96. 1
  97. 2
  98. 3
  99. 4
  100. 5
  101. function log(){
  102. console.log.apply(console, arguments);
  103. };
  104. log(1); //1
  105. log(1,2); //1 2
  106. 接下来的要求是给每一个 log 消息添加一个"(app)"的前辍,比如:
  107. 1
  108. log("hello world"); //(app)hello world
  109. 该怎么做比较优雅呢?这个时候需要想到arguments参数是个伪数组,通过 Array.prototype.slice.call 转化为标准数组,再使用数组方法unshift,像这样:
  110. 1
  111. 2
  112. 3
  113. 4
  114. 5
  115. 6
  116. function log(){
  117. var args = Array.prototype.slice.call(arguments);
  118. args.unshift('(app)');
  119. console.log.apply(console, args);
  120. };
  121. bind
  122. 说完了 apply call ,再来说说bindbind() 方法与 apply call 很相似,也是可以改变函数体内 this 的指向。
  123. MDN的解释是:bind()方法会创建一个新函数,称为绑定函数,当调用这个绑 定函数时,绑定函数会以创建它时传入 bind()方法的第一个参数作为 this,传入 bind() 方法的第二个以及以后的参数加上绑定函数运行时 本身的参数按照顺序作为原函数的参数来调用原函数。
  124. 直接来看看具体如何使用,在常见的单体模式中,通常我们会使用 _this , that , self 等保存 this ,这样我们可以在改变了上下文之后继续引用到它。 像这样:
  125. 1
  126. 2
  127. 3
  128. 4
  129. 5
  130. 6
  131. 7
  132. 8
  133. 9
  134. 10
  135. var foo = {
  136. bar : 1,
  137. eventBind: function(){
  138. var _this = this;
  139. $('.someClass').on('click',function(event) {
  140. /* Act on the event */
  141. console.log(_this.bar); //1
  142. });
  143. }
  144. }
  145. 由于 Javascript 特有的机制,上下文环境在 eventBind:function(){
  146. } 过渡到 $('.someClass').on('click',function(event) {
  147. }) 发生了改变,上述使用变量保存 this 这些方式都是有用的,也没有什么问题。当然使用 bind() 可以更加优雅的解决这个问题:
  148. 1
  149. 2
  150. 3
  151. 4
  152. 5
  153. 6
  154. 7
  155. 8
  156. 9
  157. var foo = {
  158. bar : 1,
  159. eventBind: function(){
  160. $('.someClass').on('click',function(event) {
  161. /* Act on the event */
  162. console.log(this.bar); //1
  163. }.bind(this));
  164. }
  165. }
  166. 在上述代码里,bind() 创建了一个函数,当这个click事件绑定在被调用的时候,它的 this 关键词会被设置成被传入的值(这里指调用bind()时传入的参数)。因此,这里我们传入想要的上下文 this(其实就是 foo ),到 bind() 函数中。然后,当回调函数被执行的时候, this 便指向 foo 对象。再来一个简单的栗子:
  167. 1
  168. 2
  169. 3
  170. 4
  171. 5
  172. 6
  173. 7
  174. 8
  175. 9
  176. var bar = function(){
  177. console.log(this.x);
  178. }
  179. var foo = {
  180. x:3
  181. }
  182. bar(); // undefined
  183. var func = bar.bind(foo);
  184. func(); // 3
  185. 这里我们创建了一个新的函数 func,当使用 bind() 创建一个绑定函数之后,它被执行的时候,它的 this 会被设置成 foo 而不是像我们调用 bar() 时的全局作用域。
  186. 有个有趣的问题,如果连续 bind() 两次,亦或者是连续 bind() 三次那么输出的值是什么呢?像这样:
  187. 1
  188. 2
  189. 3
  190. 4
  191. 5
  192. 6
  193. 7
  194. 8
  195. 9
  196. 10
  197. 11
  198. 12
  199. 13
  200. 14
  201. 15
  202. 16
  203. 17
  204. var bar = function(){
  205. console.log(this.x);
  206. }
  207. var foo = {
  208. x:3
  209. }
  210. var sed = {
  211. x:4
  212. }
  213. var func = bar.bind(foo).bind(sed);
  214. func(); //?
  215. var fiv = {
  216. x:5
  217. }
  218. var func = bar.bind(foo).bind(sed).bind(fiv);
  219. func(); //?
  220. 答案是,两次都仍将输出 3 ,而非期待中的 4 5 。原因是,在Javascript中,多次 bind() 是无效的。更深层次的原因, bind() 的实现,相当于使用函数在内部包了一个 call / apply ,第二次 bind() 相当于再包住第一次 bind() ,故第二次以后的 bind 是无法生效的。
  221.   
  222. applycallbind比较
  223. 那么 applycallbind 三者相比较,之间又有什么异同呢?何时使用 applycall,何时使用 bind 呢。简单的一个栗子:
  224. 1
  225. 2
  226. 3
  227. 4
  228. 5
  229. 6
  230. 7
  231. 8
  232. 9
  233. 10
  234. 11
  235. 12
  236. 13
  237. var obj = {
  238. x: 81,
  239. };
  240. var foo = {
  241. getX: function() {
  242. return this.x;
  243. }
  244. }
  245. console.log(foo.getX.bind(obj)()); //81
  246. console.log(foo.getX.call(obj)); //81
  247. console.log(foo.getX.apply(obj)); //81
  248. 三个输出的都是81,但是注意看使用 bind() 方法的,他后面多了对括号。
  249. 也就是说,区别是,当你希望改变上下文环境之后并非立即执行,而是回调执行的时候,使用 bind() 方法。而 apply/call 则会立即执行函数。
  250. 再总结一下:
  251. apply call bind 三者都是用来改变函数的this对象的指向的;
  252. apply call bind 三者第一个参数都是this要指向的对象,也就是想指定的上下文;
  253. apply call bind 三者都可以利用后续参数传参;
  254. bind 是返回对应函数,便于稍后调用;apply call 则是立即调用

href=”#”与href=”javascript:void(0)”的区别

  1. "#"包含了一个位置信息
  2. 默认的锚点是#top 也就是网页的上端
  3. javascript:void(0) 仅仅表示一个死链接
  4. 这就是为什么有的时候页面很长浏览链接明明是#可是跳动到了页首
  5. javascript:void(0) 则不是如此
  6. 所以调用脚本的时候最好用void(0)

全选,全不选,反选

这里写图片描述

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>全选,不全选,反选</title>
  5. <meta charset="UTF-8" />
  6. <script type="text/javascript" src="js/jquery-2.2.4.min.js"></script>
  7. <script language="javascript" type="text/javascript">
  8. $(function() {
  9. $("#selectAll").click(function() {
  10. //全选
  11. $(":checkbox").prop("checked", true);
  12. });
  13. $("#unSelect").click(function() {
  14. //全不选
  15. $(":checkbox").prop("checked", false);
  16. });
  17. $("#reverse").click(function() {
  18. //反选
  19. $(":checkbox").each(function() {
  20. $(this).prop("checked", !$(this).prop("checked"));
  21. });
  22. });
  23. });
  24. </script>
  25. </head>
  26. <body>
  27. <div id="playList">
  28. <input type="checkbox" value="歌曲1" />歌曲1<br />
  29. <input type="checkbox" value="歌曲2" />歌曲2<br />
  30. <input type="checkbox" value="歌曲3" />歌曲3<br />
  31. <input type="checkbox" value="歌曲4" />歌曲4<br />
  32. <input type="checkbox" value="歌曲5" />歌曲5<br />
  33. <input type="checkbox" value="歌曲6" />歌曲6
  34. </div>
  35. <input type="button" value="全选" id="selectAll" />
  36. <input type="button" value="全不选" id="unSelect" />
  37. <input type="button" value="反选" id="reverse" />
  38. </body>
  39. </html>

三种匿名自执行写法

  1. 如果你发现有些人写函数这样搞,不要惊慌,也不要觉得他高大上鸟不起
  2. +function(ROOT, Struct, undefined) {
  3. ...
  4. }(window, function() {
  5. function Person() {
  6. }
  7. })
  8. ()(), !function() {
  9. }() +function() {
  10. }() 三种函数自执行的方式^_^

jquery 一个容易问的考点

这里写图片描述

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <script type="text/javascript" src="js/jquery-2.2.4.min.js"></script>
  5. <meta charset="UTF-8" />
  6. <title>Document</title>
  7. <style type="text/css">
  8. </style>
  9. <script type="text/javascript">
  10. $(function() {
  11. $("#id0>li:not(:has(ul))").css('border', '1px solid red');
  12. })
  13. </script>
  14. </head>
  15. <body>
  16. <ul id="id0">
  17. <li>list item 1</li>
  18. <li>list item 2
  19. <ul>
  20. <li>list item 2-a</li>
  21. <li>list item 2-b</li>
  22. </ul>
  23. </li>
  24. <li>list item 3</li>
  25. <li>list item 4</li>
  26. </ul>
  27. </body>
  28. </html>

jquery position()和offset()

这里写图片描述

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <script type="text/javascript" src="js/jquery-2.2.4.min.js"></script>
  5. <meta charset="UTF-8" />
  6. <title>Document</title>
  7. <style type="text/css">
  8. html {
  9. padding: 0px;
  10. margin: 0px;
  11. }
  12. body {
  13. margin: 3px;
  14. padding: 6px;
  15. }
  16. div#main_1 {
  17. width: 200px;
  18. height: 200px;
  19. background: red;
  20. position: relative;
  21. }
  22. div#main_2 {
  23. width: 50px;
  24. height: 50px;
  25. background: green;
  26. position: absolute;
  27. left: 30px;
  28. top: 10px;
  29. }
  30. </style>
  31. <script type="text/javascript">
  32. $(function() {
  33. console.log($("#main_2").position().left); //获取匹配元素相对父元素的偏移。
  34. console.log($("#main_2").position().top);
  35. console.log($("#main_2").offset().left); //获取匹配元素在当前视口的相对偏移。
  36. console.log($("#main_2").offset().top);
  37. console.log($("#main_2").css('left'));
  38. console.log($("#main_2").css('top'));
  39. })
  40. </script>
  41. </head>
  42. <body>
  43. <div id="main_1">
  44. <div id="main_2">
  45. </div>
  46. </div>
  47. </body>
  48. </html>

js 实现继承

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title></title>
  6. <script type="text/javascript">
  7. function Base(name, base) {
  8. this.name = name;
  9. this.base = base;
  10. this.sayHello = function() {
  11. alert('Hello ' + this.name);
  12. };
  13. }
  14. function Ase(name, base) {
  15. this.stu = Base; //重要
  16. this.stu(name, base); //重要
  17. this.sayHelloTo = function() {
  18. alert('Hello ' + this.name+this.base*2);
  19. };
  20. }
  21. var a = new Base('李四',28);
  22. var b = new Ase('张三',55);
  23. a.sayHello();
  24. b.sayHello();
  25. b.sayHelloTo();
  26. </script>
  27. </head>
  28. <body>
  29. </body>
  30. </html>
  31. <!doctype html>
  32. <html lang="en">
  33. <head>
  34. <meta charset="UTF-8" />
  35. <title>Document</title>
  36. <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
  37. <script type="text/javascript">
  38. function sty() {
  39. }
  40. sty.prototype = function() {
  41. add = function(x, y) {
  42. return x + y;
  43. },
  44. subtract = function(x, y) {
  45. return x - y;
  46. }
  47. return {
  48. add: add,
  49. subtract: subtract
  50. }
  51. }();
  52. set
  53. var aa = new sty();
  54. alert(aa.add(1, 2));
  55. alert(aa.subtract(1, 2));
  56. </script>
  57. </head>
  58. <body>
  59. </body>
  60. </html>
  61. <!--
  62. <!doctype html>
  63. <html lang="en">
  64. <head>
  65. <meta charset="UTF-8" />
  66. <title>Document</title>
  67. <script type="text/javascript">
  68. var test=[1,2,3,4,5,6,7,8,9,10];
  69. window.onload=function(){
  70. combination(test, 4);
  71. }
  72. function combination(array, NumberCount){
  73. var NUMBER_N=NumberCount;
  74. var LENGTH=array.length;
  75. var numberN=1;//当前填入N的是数字编号
  76. var arrayN=[];//N个数的数组
  77. var arrayResult=[];//组合数的二维数组
  78. select(array.slice(0,LENGTH),arrayN,arrayResult,LENGTH,numberN,NUMBER_N);
  79. }
  80. function select(array,arrayN,arrayResult,LENGTH,numberN,NUMBER_N){
  81. var length=array.length;
  82. var disFlag=NUMBER_N-numberN + 1 - length;
  83. if(disFlag==0){
  84. var index=arrayN.length-1;
  85. var focus=arrayN[index];
  86. arrayN=arrayN.concat(array);
  87. arrayResult.push(arrayN);
  88. numberN--;
  89. arrayN=arrayN.slice(0,index);
  90. var array_next=test.slice(0,LENGTH);
  91. for(var j =0;j<LENGTH;j++){
  92. var target=array_next.shift();
  93. if(target==focus){
  94. break;}
  95. }
  96. select(array_next,arrayN,arrayResult,LENGTH,numberN,NUMBER_N);
  97. }else if(disFlag<0){
  98. if(numberN<NUMBER_N){
  99. numberN++;
  100. arrayN.push(array[0]);
  101. var arrayNew=array;
  102. arrayNew.splice(0,1);
  103. select(arrayNew,arrayN,arrayResult,LENGTH,numberN,NUMBER_N);
  104. }else{
  105. numberN--;
  106. for(var i in array){
  107. var arrayN_new=arrayN.slice(0,arrayN.length);
  108. arrayN_new.push(array[i]);
  109. arrayResult.push(arrayN_new);
  110. }
  111. arrayN.splice(arrayN.length-1,arrayN.length)
  112. select(array,arrayN,arrayResult,LENGTH,numberN,NUMBER_N);
  113. }
  114. }else{
  115. for(key in arrayResult){
  116. var p = document.createElement("p");
  117. p.innerText=arrayResult[key];
  118. document.body.appendChild(p);
  119. }
  120. return;
  121. }
  122. }
  123. </script>
  124. </head>
  125. <body>
  126. </body>
  127. </html>-->

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

这里写图片描述

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Document</title>
  6. <script type="text/javascript">
  7. (function(n){
  8. for (var i = 0; i <n; i++) {
  9. setTimeout(function(){
  10. console.log(i);
  11. },1)
  12. }
  13. })(6)
  14. </script>
  15. </head>
  16. <body>
  17. </body>
  18. </html>

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  1. <script type="text/javascript">
  2. function add(n){
  3. this.n=n;
  4. this.m=function(){
  5. return this.n/2;
  6. }
  7. }
  8. var A=new add(20);
  9. alert(A.n+" "+A.m()); //20 10
  10. A.n=40;
  11. alert(A.n+" "+A.m()); //40 20
  12. </script>

这里写图片描述

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <script type="text/javascript" src="js/jquery-2.1.4.min.js"></script>
  6. <title>Document</title>
  7. <style type="text/css">
  8. </style>
  9. <script type="text/javascript">
  10. var a;
  11. console.log("1:" + typeof a);
  12. var b = null;
  13. console.log("2:" + typeof b);
  14. var c = undefined;
  15. console.log("3:" + typeof c);
  16. var d = new Object;
  17. console.log("4:" + typeof d);
  18. var e = function() {
  19. };
  20. console.log("5:" + typeof e);
  21. var f = {};
  22. console.log("6:" + typeof f);
  23. var g = '';
  24. console.log("7:" + typeof g);
  25. var h = [];
  26. console.log("8:" + typeof h);
  27. var i = true;
  28. console.log("9:" + typeof i);
  29. var j = 123;
  30. console.log("10:" + typeof j);
  31. var k = NaN;
  32. console.log("11:" + typeof k);
  33. var l = /^[-+]?\d+$/;
  34. console.log("12:" + typeof l);
  35. </script>
  36. </head>
  37. <body>
  38. </body>
  39. </html>

这里写图片描述

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Document</title>
  6. <style type="text/css">
  7. #main {
  8. width: 200px;
  9. height: 200px;
  10. border-bottom-left-radius: 100%;
  11. background: red;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="main">
  17. </div>
  18. </body>
  19. </html>

发表评论

表情:
评论列表 (有 0 条评论,219人围观)

还没有评论,来说两句吧...

相关阅读

    相关 js

    判断一个变量var是否是数组,你需要使用Array.isArray(var),不能用typeof,否则弹出object 数组排序:\[1,2,5,10\].sort((a, b