JavaScript盲点记录----(一)
文章目录
最近在练习JavaScript题目时发现有些实现自己立即并不能想出来,而且有些常见的方法的用法理解是有错误的如parseInt,所以记录一下。这是第一篇,定要温故而知新 。^_^
解析url ,window.location.search 是从?向后的部分
12345678910111213141516171819202122function getUrlParam(sUrl, sKey) {var search = sUrl.split("?")[1].split("#")[0];var searchArr = search.split("&");var result={};for (var i = 0; i < searchArr.length; i++) {var temp = searchArr[i].split("=");if(temp[0]){if(!result[temp[0]]){result[temp[0]] =[];}result[temp[0]].push(decodeURIComponent(temp[1]));}}if(sKey){if(!result[sKey]){result =" ";}else if(result[sKey].length ==1){result = result[sKey][0];}}return result;}查询共同的父节点
123456789101112131415161718192021222324252627282930function commonParentNode(oNode1, oNode2) {var oNode1Parents = [oNode1];var oNode2Parents = [oNode2];while (oNode1.parentNode) {oNode1Parents.push(oNode1.parentNode);oNode1 = oNode1.parentNode;}while (oNode2.parentNode) {oNode2Parents.push(oNode2.parentNode);oNode2 = oNode2.parentNode;}var len1 = oNode1Parents.length;var len2 = oNode2Parents.length;for (var i = 0; i < len1; i++) {for (var j = 0; j < len2; j++) {if (oNode1Parents[i] == oNode2Parents[j]) {return oNode1Parents[i];}}}}// 更好的方法 node.contains() 方法用来判断一个节点是否是node的后代function commonParentNode(oNode1, oNode2) {if (oNode1.contains(oNode2)) {return oNode1;} else {return commonParentNode(oNode1.parentNode, oNode2);}}根据包名,在指定的空间中创建对象
12345678910function namespace(oNamespace, sPackage) {var packageArr = sPackage.split('.');var parentNode = oNamespace;for (var i = 0; i < packageArr.length; i++) {parentNode[packageArr[i]] =typeof parentNode[packageArr[i]] =='object'? parentNode[packageArr[i]]:{};parentNode = parentNode[packageArr[i]];}return oNamespace;}数组去重
1234567891011121314151617181920212223var arr = [false, true, undefined, null, NaN, 0, 1, {}, {}, 'a', 'a', NaN];var result = [false, true, undefined, null, NaN, 0, 1, {}, {}, 'a'];Array.prototype.uniq = function () {var resArr = [];var flag = true;for (var i = 0; i < this.length; i++) {if (resArr.indexOf(this[i]) == -1) {if (this[i] != this[i]) { //排除 NaNif (flag) {resArr.push(this[i]);flag = false;}} else {resArr.push(this[i]);}}}return resArr;}Array.prototype.uniq = function () {return Array.from(new Map(this))}判断邮箱格式是否正确
1234function isAvailableEmail(sEmail) {var reg = /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/;return reg.test(sEmail);}将 rgb 颜色字符串转换为十六进制的形式,如 rgb(255, 255, 255) 转为 #ffffff
1234567891011121314151617function rgb2hex(sRGB) {var regexp = /rgb\((\d+),\s*(\d+),\s*(\d+)\)/;var ret = sRGB.match(regexp);if (!ret) {return sRGB;} else {var str = '#';for (var i = 1; i <= 3; i++) {var m = parseInt(ret[i]);if (m <= 255 && m >= 0) {str += (m < 16 ? '0' + m.toString(16) : m.toString(16));} else {return sRGB;}} return str;}}parseInt(string,radix),是将string看成是以radix 为基数的字符串,不是转成以radix为基数的数字
radix:表示要解析的数字的基数。该值介于 2 ~ 36 之间。
如果省略该参数或其值为 0,则数字将以 10 为基础来解析。如果它以 “0x” 或 “0X” 开头,将以 16为基数。
如果该参数小于 2 或者大于 36,则 parseInt() 将返回 NaN
parseInt(‘12px’,10);// 12 按10进制去处理字符串,碰到非数字字符,会将后面的全部无视将整型数值以特定基数转换成它的字符串值可以使用 intValue.toString(radix).
找出数组中重复出现的元素
12345678910// 方法一 方法二 : 用object 去计数,方法三:用 数组去计数function duplicates(arr) {var result = [];arr.forEach(function (elem) {if (arr.indexOf(elem) != arr.lastIndexOf(elem) && result.indexOf(elem) == -1) {result.push(elem);}});return result;}使用闭包,对数组中的每个值执行函数
12345678function makeClosures(arr, fn) {var result =[];for(let i=0;i<arr.length;i++){result[i] =function(){return fn(arr[i]);}}}关于arguments
123456789101112131415161718192021// 将arguments变成数组Array.apply(null,arguments);Array.prototype.slice.call(arguments,0);Array.prototype.splice.call(arguments,0,arguments.length)// 取arguments的第一个值[].push(arguments)```11. Curry化是把接受多个参数的函数变换成接收一个单一参数(这个参数就是那个要接收多个参数的函数)的函数```javascriptfunction curryIt(fn) {var n = fn.length;var args = [];return function lx (arg) {args.push(arg);// 只取一个参数值if (args.length < n) {return lx;} else return fn.apply(null, args);}}数组与对象的 深拷贝
123456789101112131415var cloneObj = function (obj) {var str, newobj = obj.constructor === Array ? [] : {};if (typeof obj !== 'object') {return;} else if (window.JSON) {str = JSON.stringify(obj), //系列化对象newobj = JSON.parse(str); //还原} else {for (var i in obj) {newobj[i] = typeof obj[i] === 'object' ?cloneObj(obj[i]) : obj[i];}}return newobj;};折半查找: 折半查找即二分查找 。使用折半查找的数组必须是对于有序的数组,使用无序的数组是不可以的。
123456789101112131415function Search(arrs, findVal) {var low = 0, high = arrs.length - 1;var mid;while (low <= high) {mid = Math.floor((low + high) / 2);if (arrs[mid] == findVal) { //找到则返回return mid;}if (arrs[mid] > findVal) //大于目标,找左边high = mid - 1;else //小于目标,找右边low = mid + 1;}return -1;}折半查找的递归实现
1234567891011121314function binarySearch(arr,findVal,leftIndex,rightIndex) {if(leftIndex > rightIndex) {return -1;}var midIndex = Math.floor((leftIndex + rightIndex)/2);var midVal = arr[midIndex];if(midVal > findVal) {binarySearch(arr,findVal,leftIndex,midIndex - 1);} else if(midVal < findVal) {binarySearch(arr,findVal,midIndex+1,rightIndex);} else {return mid;}}