├── images └── 1.png ├── .idea ├── misc.xml ├── vcs.xml ├── modules.xml ├── javavscript代码段.iml └── workspace.xml ├── flatten.js ├── deepCopy.js ├── debounce.js ├── shallowCopy.js ├── type.js └── README.md /images/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GIScyw/javascript-code-segment/HEAD/images/1.png -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /flatten.js: -------------------------------------------------------------------------------- 1 | //数组的扁平化 2 | function flatten(arr) { 3 | var result = []; 4 | for (var i = 0, len = arr.length; i < len; i++) { 5 | if (Array.isArray(arr[i])) { 6 | result = result.concat(flatten(arr[i])) 7 | } 8 | else { 9 | result.push(arr[i]) 10 | } 11 | } 12 | return result; 13 | } 14 | 15 | 16 | -------------------------------------------------------------------------------- /deepCopy.js: -------------------------------------------------------------------------------- 1 | //深拷贝只需要在拷贝的时候判断一下属性值得类型,如果是对象,就递归调用深拷贝函数就行了 2 | var deepCopy = function(obj) { 3 | if (typeof obj !== 'object') return; 4 | var newObj = obj instanceof Array ? [] : {}; 5 | for (var key in obj) { 6 | if (obj.hasOwnProperty(key)) { 7 | newObj[key] = typeof obj[key] === 'object' ? deepCopy(obj[key]) : obj[key]; 8 | } 9 | } 10 | return newObj; 11 | } 12 | -------------------------------------------------------------------------------- /debounce.js: -------------------------------------------------------------------------------- 1 | function debounce(fn, wait) { 2 | var timeout = null; 3 | return function() { 4 | if(timeout !== null) 5 | { 6 | clearTimeout(timeout); 7 | } 8 | timeout = setTimeout(fn, wait); 9 | } 10 | } 11 | // 处理函数 12 | function handle() { 13 | console.log(Math.random()); 14 | } 15 | // 滚动事件 16 | window.addEventListener('scroll', debounce(handle, 1000)); 17 | -------------------------------------------------------------------------------- /shallowCopy.js: -------------------------------------------------------------------------------- 1 | //该拷贝函数只拷贝对象或者数组,浅拷贝就是遍历对象,然后把属性和属性值都放在一个新的对象就行了 2 | var shallowCopy = function(obj) { 3 | // 只拷贝对象 4 | if (typeof obj !== 'object') return; 5 | // 根据obj的类型判断是新建一个数组还是对象 6 | var newObj = obj instanceof Array ? [] : {}; 7 | // 遍历obj,并且判断是obj的属性才拷贝 8 | for (var key in obj) { 9 | if (obj.hasOwnProperty(key)) { 10 | newObj[key] = obj[key]; 11 | } 12 | } 13 | return newObj; 14 | } 15 | -------------------------------------------------------------------------------- /.idea/javavscript代码段.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /type.js: -------------------------------------------------------------------------------- 1 | // 该类型判断函数可以判断六中基本数据类型以及Boolean Number String Function Array Date RegExp Object Error, 2 | // 其他类型因为遇到类型判断的情况较少所以都会返回object,不在进行详细的判断 3 | // 比如ES6新增的Symbol,Map,Set等类型 4 | var classtype = {}; 5 | 6 | 7 | "Boolean Number String Function Array Date RegExp Object Error".split(" ").map(function(item) { 8 | classtype["[object " + item + "]"] = item.toLowerCase(); 9 | }) 10 | 11 | 12 | function type(obj) { 13 | // 解决IE6中null和undefined会被Object.prototype.toString识别成[object Object] 14 | if (obj == null) { 15 | return obj + ""; 16 | } 17 | 18 | //如果是typeof后类型为object下的细分类型(Array,Function,Date,RegExp,Error)或者是Object类型,则要利用Object.prototype.toString 19 | //由于ES6新增的Symbol,Map,Set等类型不在classtype列表中,所以使用type函数,返回的结果会是object 20 | return typeof obj === "object" || typeof obj === "function" ? 21 | classtype[Object.prototype.toString.call(obj)] || "object" : 22 | typeof obj; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 72 | 73 | 82 | 83 | 84 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 |