chenhuiyj / ec-do Goto Github PK
View Code? Open in Web Editor NEW实例为日常开发常用的小实例,包括数组去重,打乱数组,字母大小写转换,cookie操作的封装等。
License: MIT License
实例为日常开发常用的小实例,包括数组去重,打乱数组,字母大小写转换,cookie操作的封装等。
License: MIT License
function setUrlParam(url, obj) {
let _rs = Object.keys(obj).filter(key => obj[key] !== ''&&obj[key] !== null&&obj[key] !== undefined)
.map(key => encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]))
.join('&');
return `${url}?${_rs}`
}
基于 https://segmentfault.com/a/1190000010225928 这篇文章(比较好辨识……)
库中部分 api 有原生方案的选择:
trim(str, type)
case 2, 3, 4String.prototype.trim
String.prototype.trimLeft
String.prototype.trimRight
repeatStr(str, count)
String.prototype.repeat
sumArr(arr)
[1, 2, 3, 4].reduce((x, sum) => x + sum, 0)
getEleCount (obj, ele)
要用 ===
?
hasClass(obj, classStr)
obj.classList.contains
addClass(obj,classStr)
obj.classList.add
removeClass(obj, classStr)
obj.classList.remove
replaceClass(obj,classStr)
obj.classList.replace
siblings(obj)
Array.from(obj.parentNode.children).filter(z => z !== obj)
css(obj, json)
Object.assign(obj, json)
getUrlPrmt(url)
& setUrlPrmt(obj)
const url = new URL('https://github.com/chenhuiYj/ec-do/issues/new?a=1');
url.searchParams.get('a') // get
url.searchParams.set('b', '44') // set
urlX.href // https://github.com/chenhuiYj/ec-do/issues/new?a=1&f=1
替换 prototype 时不少库(如 jQuery)的做法是这样,不会造成覆盖原生方法:
String.prototype.trim = String.prototype.trim || function(type) { ... }
还有些其它问题不属于【原生方案】这一项,就不写在这了。
当输入100000的时候会把“万”字去掉,变成人民币壹拾元整。类似的还有10000,20000,300000这些的数字,最后的replace正则有问题
function PrefixInteger(num, length = 2) { return (Array(length).join("0") + num).slice(-length); }
function strToNode(str = "<div class='strToNode'></div>") { let tempNode = document.createElement('div'); tempNode.innerHTML = str; return tempNode.firstElementChild; }
一、字符串相关:
1.关键字加标签findKey()方法:
调用:findKey('上海自来水来自海上','上海 海','p')
输出:<p>上海</p>自来水来自<p>海</p>上
问题:上海中也包含海,所以我可能期待的结果是:<p>上<p>海</p></p>自来水来自<p>海</p>上
,当然每个人有不同的想法,我觉得你之前这个也不错,建议而已。
2.过滤字符串filterStr()方法:
原文:for(var i=0,len=_spstr.length;i<len;i++){ if(regText.indexOf(_spstr[i])===-1){ _regText+=_spstr[i]; } else{ _regText+='\\'+_spstr[i]; } }
问题:此for循环中的i与原文中上面的i可能冲突,导致结果出错。
建议:把此for循环中的i改为j,len改为length,期待改进。
二、数组相关:
1.数组扁平化steamroller()方法:
原文: if (Array.isArray(arr[i])) { newArr.push.apply(newArr, steamroller(arr[i])); } else {
问题:少一个this,另一种写法同理,期待改进。
三、数据类型相关:
1.数据类型判断istype()方法:
原文:if(_type){ var _type = type.toLowerCase(); }
问题:if判断语句中的_type应为type,期待改进。
四、ajax相关:
1.ajax()方法:
原文:if (xmlHttp.readyState == 4 && xmlHttp.status == 200) { obj.success(xmlHttp.responseText); } else { obj.error(xmlHttp.responseText); }
问题:当readyState的状态为2和3的时候也会触发请求,故error事件并不能捕捉到异常,期待改进。
五、图片加载相关:
1.图片加载失败aftLoadImg()方法:
原文:aftLoadImg: function (obj, url, cb) { var oImg = new Image(), _this = this; oImg.src = url; .... },
问题:当图片加载失败时,可能会造成闪烁(一直请求加载,之前遇到过类似问题)
建议:aftLoadImg: function (obj, url, cb) { var oImg = new Image(), _this = this; oImg.onerror = ""; oImg.src = url; oImg.onerror = null; .... },
2.图片懒加载loadImg()方法:
问题:只能加载前三张图片,期待改进。
六、DOM操作相关:
1.显示与隐藏show()方法:
原文:obj.style.display = "";
问题:如果这个DOM对象本身的display属性为none的话,就不起效,但是又不能把他简单的设置为obj.style.display = "block";
因为要保持它原本的display属性,期待改进。
2.设置文本内容html()方法:
建议:此方法名由"设置文本内容"改为"设置HTML内容的值",再新增一个“设置元素内容的文本”text()方法。ps:innerHTML是所有浏览器都支持的,innerText是IE浏览器和chrome浏览器支持的,Firefox浏览器不支持,其实innerHTML是W3C组织规定的属性,而innerText属性是IE浏览器自己的属性,不过后来的浏览器部分实现这个属性罢了。
感谢你的贡献,我自己也在你本来的代码里加了一些处理时间相关的方法,感觉时间处理比较常见,当然还有别的,期待你的更新。
本人水平有限,以上建议和意见可能不成熟,望见谅。
var covText = this.sumText / length;
this.sumText是undefined, length为0;
应该是sumText/arr.length吧?
/**
* @description 设置或获取文本内容
* @param obj
* @return {*}
*/
text(obj) {
if (arguments.length === 1) {
return obj.innerHTML;
} else if (arguments.length === 2) {
obj.innerHTML = this.filterStr(arguments[1], 'html');
}
return this;
},
text方法根据传参的数量不同,功能也不同,如果传2个参数,就是赋值操作,但是如果执行赋值操作,目前会报错 "this.filterStr is not a function"。
查阅filterStr方法,返回的是一个包含两个方法的对象,所以text调用this.filterStr需要修改。
我觉得这个函数有个疑问, 我开始以为调用randomWord()
函数时传递的数字, 是可以得到我传递时对应的字符长度. 所以我这里有点疑问, 我试了下, 发现得到的不是我传递的数字长度. 那么传递的数字的作用是什么?
麻烦您解答下.
打扰了.
Lines 610 to 613 in 9e079f3
这里错了吧,2个 obj.success
//数组去重:利用key的唯一性
function removeRepetition (a) { var rs = [],hash = {}; for(var key in a){ if(a[key]!=null && !hash[a[key]]){ rs.push(a[key]); hash[a[key]] = true; } } return rs; }
对于其中的html方法,具体的调用方式是什么样子的呢?
是html(documrnt.getElementById('demo'),'123')
这种还是html(documrnt.getElementById('demo'))
这样的呢?
随机码count取值范围是2-36不应该是0-36;建议修改;
filterEmjoy函数的描述写错了,写成“过滤字符串的大写字母”了
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.