想写点啥,又被熊孩子吵得头大,感觉心力交瘁有心无力……回头再编辑。
Finally, litle peaceful time for myself. What I have done today technically is nothing - went through some basic methods about string/array/object again which makes me sick especilly when you do that in your car. When I read about how this guy who works in NetEase harnesses reduce as a rag doll, it makes me doubting again if I have a functional brain or I have already got Alzheimer's.
And the other thing is changed theme for this experimental blog, although I still haven't finished my own website's いろいろ problems.
Then let's just go through some fundamental stuff.
1) Array methods
基于ES6,改变自身值的方法一共有9个,分别为pop、push、reverse、shift、sort、splice、unshift,以及两个ES6新增的方法copyWithin 和 fill。
基于ES7,不会改变自身的方法一共有9个,分别为concat、join、slice、toString、toLocateString、indexOf、lastIndexOf、未标准的toSource以及ES7新增的方法includes。
遍历:
基于ES6,不会改变自身的方法一共有12个,分别为forEach、every、some、filter、map、reduce、reduceRight 以及ES6新增的方法entries、find、findIndex、keys、values。
2) String methods
字符串原型方法分为两种,一种是html无关的方法,一种是html有关的方法。我们先看第一种。但是无论字符串方法如何厉害,都==不==至于强大到可以==改变原字符串==。
通常,字符串中,==常用的方法==就charAt、indexOf、lastIndexOf、match、replace、search、slice、split、substr、substring、toLowerCase、toUpperCase、trim、valueof 等这些。熟悉它们的语法规则就能熟练地驾驭字符串。
concat
concat 的性能表现不佳,强烈推荐使用赋值操作符(+或+=)代替 concat。”+” 操作符大概快了 concat 几十倍。
slice
slice() 方法提取字符串的一部分,并返回新的字符串。该方法有些类似 Array.prototype.slice 方法。
toString valueOf
这两个方法都是返回字符串本身。
语法:
str.toString(),str.valueOf()
对于对象而言,toString和valueOf也是非常的相似,它们之间有着细微的差别,请尝试运行以下一段代码:
1 | var x = { |
当 “+” 操作符一边为数字时,对象x趋向于转换为数字,表达式会优先调用 valueOf 方法,如果调用数组的 join 方法,对象x趋向于转换为字符串,表达式会优先调用 toString 方法。
trim 不支持IE9以下的低版本IE浏览器
trim() 方法清除字符串首尾的空白并返回。
语法:str.trim()
3) Object methods
常用的API主要有Object.prototype.toString(),Object.prototype.hasOwnProperty(), Object.getPrototypeOf(obj),Object.create(),Object.defineProperty,Object.keys(obj),Object.assign()。
Object.keys() and Array.forEach() 结合使用,实现object的foreach循环遍历。
https://gomakethings.com/the-es6-way-to-loop-through-objects-with-vanilla-javascript/