Git Product home page Git Product logo

ec-do's Issues

一些疑问

  1. 数组求和那个函数用reduce方法不好吗?
  2. 随机码那个函数加上随机码长度更实用吧?
    3.得到n1-n2下标的数组,用slice方法好点吧?

增加 补零和字符串转DOM?!

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; }

关于randomWord函数我有个不太明白的地方?

我觉得这个函数有个疑问, 我开始以为调用randomWord()函数时传递的数字, 是可以得到我传递时对应的字符长度. 所以我这里有点疑问, 我试了下, 发现得到的不是我传递的数字长度. 那么传递的数字的作用是什么?
麻烦您解答下.
打扰了.

大概看了下ec-do-2.0.0.js的代码,有点建议

  1. 可以将不同功用的归类,比如用于字符串的就是StringUtils,用于DOM的就是DOMUtils这样子,更清晰
  2. 我觉得不应该将函数中的Type用数字作区分,比如changeCase函数,用1表示首字母大写,用2表示首字母小写,这样子代码的可读性会很差,建议使用字符串类型,或者抽出来成为constant

ajax 错误

ec-do/ec-do.js

Lines 610 to 613 in 9e079f3

obj.success = obj.success || function () {
};
obj.success = obj.error || function () {
};

这里错了吧,2个 obj.success

原生方案

基于 https://segmentfault.com/a/1190000010225928 这篇文章(比较好辨识……)

库中部分 api 有原生方案的选择:

trim(str, type) case 2, 3, 4

  • String.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) { ... }

还有些其它问题不属于【原生方案】这一项,就不写在这了。

有一处方法使用方式不懂

对于其中的html方法,具体的调用方式是什么样子的呢?
html(documrnt.getElementById('demo'),'123')这种还是html(documrnt.getElementById('demo'))这样的呢?

人民币转换大写函数Bug

当输入100000的时候会把“万”字去掉,变成人民币壹拾元整。类似的还有10000,20000,300000这些的数字,最后的replace正则有问题

设置url参数这样如何

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}`
}

数组去重,这种思路如何?

//数组去重:利用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; }

一些建议和BUG,共同完善

一、字符串相关:
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浏览器自己的属性,不过后来的浏览器部分实现这个属性罢了。

感谢你的贡献,我自己也在你本来的代码里加了一些处理时间相关的方法,感觉时间处理比较常见,当然还有别的,期待你的更新。

本人水平有限,以上建议和意见可能不成熟,望见谅。

text方法内调用filterStr方法的问题

   /**
     * @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需要修改。

平均值哪里BUG

var covText = this.sumText / length;
this.sumText是undefined, length为0;
应该是sumText/arr.length吧?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.