Module: lang

本模块提供基础方法。
Source:

Methods

(static) cloneJSON(obj) → {Any}

深度克隆指定对象(仅限 JSON 支持的数据类型)。
Parameters:
Name Type Description
obj Any 指定对象。
Author:
  • liumin
Source:
Returns:
克隆结果。
Type
Any
Example
cloneJSON({ a: 1, b: 2 }); // { a: 1, b: 2 }

(static) extend(target, …sourceopt) → {Any}

把源对象的属性(own property)扩展到目标对象(同 Object.assign)。
Parameters:
Name Type Attributes Description
target Any 目标对象。
source Any <optional>
<repeatable>
源对象。若有同名属性,则后者覆盖前者。
Author:
  • luoliquan
Source:
Returns:
目标对象。
Type
Any

(static) hasOwnProp(obj, prop) → {boolean}

检查指定对象是否具有某个 own property(ESLint 不推荐直接使用 obj.hasOwnProperty)。
Parameters:
Name Type Description
obj Any 指定对象。
prop string 属性名。
Author:
  • luoliquan
Source:
Returns:
指定对象是否具有某个 own property。
Type
boolean

(static) isArrayLike(obj) → {boolean}

检查指定对象是否为类数组结构。
Parameters:
Name Type Description
obj Any 指定对象。
Author:
  • luoliquan
Source:
Returns:
检查指定对象是否为类数组结构。
Type
boolean
Example
isArrayLike([]); // true
isArrayLike(document.getElementsByTagName('body')); // true
isArrayLike({}); // false

(static) isEmptyData(value) → {boolean}

检查指定值是否为空数据。以下情况会判断为空数据: null 或者 undefined; 数组结构,但长度为 0; 空字符串或仅包含空白字符的字符串; 没有 own property 的纯对象。
Parameters:
Name Type Description
value Any 指定值。
Author:
  • luoliquan
Source:
Returns:
指定值是否为空数据。
Type
boolean
Example
isEmptyData(null); // true
isEmptyData([]); // true
isEmptyData(''); // true
isEmptyData({}); // true
isEmptyData({ a: 1 }); // false
isEmptyData([1]); // false

(static) tryParseJSON(str, onErroropt) → {Any}

尝试把指定字符串解析为 JSON 对象。
Parameters:
Name Type Attributes Description
str string 指定字符串。
onError function <optional>
解析出错时执行的函数。
Author:
  • luoliquan
Source:
Returns:
解析结果,解析失败时返回 undefined。
Type
Any
Example
tryParseJSON('ss&&**'); // undefined
tryParseJSON('{"a": 1}'); // { a: 1 }