🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
:-: 续接(一) <div class="cline"></div> ```js /** * Remove an item from an array */ function remove (arr, item) { if (arr.length) { var index = arr.indexOf(item); if (index > -1) { return arr.splice(index, 1) } } } ``` 移除数组中的某一项,该方法可通用化。 <div class="cline"></div> ```js /** * Check whether the object has the property. */ var hasOwnProperty = Object.prototype.hasOwnProperty; function hasOwn (obj, key) { return hasOwnProperty.call(obj, key) } ``` 检查属性是否是对象的原型属性。 关于`hasOwnProperty`的几点说明: >[info] >**概念**:用于指示一个对象自身(不包括原型链)是否具有指定名称的属性。如果有,返回`true`,否则返回`false` >**语法**:`obj.hasOwnProperty(prop)` >**描述**:所有继承了` Object `的对象都会继承到 `hasOwnProperty` 方法。这个方法可以用来检测一个对象是否含有特定的自身属性;和 in 运算符不同,该方法会**忽略掉那些从原型链上继承到的属性**。 <div class="cline"></div> ```js /** * Create a cached version of a pure function. */ function cached (fn) { var cache = Object.create(null); return (function cachedFn (str) { var hit = cache[str]; return hit || (cache[str] = fn(str)) }) } /** * Camelize a hyphen-delimited string. */ var camelizeRE = /-(\w)/g; var camelize = cached(function (str) { return str.replace(camelizeRE, function (_, c) { return c ? c.toUpperCase() : ''; }) }); /** * Capitalize a string. */ var capitalize = cached(function (str) { return str.charAt(0).toUpperCase() + str.slice(1) }); /** * Hyphenate a camelCase string. */ var hyphenateRE = /\B([A-Z])/g; var hyphenate = cached(function (str) { return str.replace(hyphenateRE, '-$1').toLowerCase() }); ```