xe-utils
Version:
JavaScript 函数库、工具类
44 lines (42 loc) • 1.42 kB
JavaScript
import helperDeleteProperty from './helperDeleteProperty.js';import helperFunctionApply from './helperFunctionApply.js';import isPlainObject from './isPlainObject.js';import isObject from './isObject.js';import isArray from './isArray.js';import isNull from './isNull.js';import assign from './assign.js';import objectEach from './objectEach.js';/**
* 清空对象
*
* @param {Object} obj 对象
* @param {*} defs 默认值,如果不传(清空所有属性)、如果传对象(清空并继承)、如果传值(给所有赋值)
* @param {Object/Array} assigns 默认值
* @return {Object}
*/
function clear (obj, defs, assigns) {
if (obj) {
var len
var isDefs = arguments.length > 1 && (isNull(defs) || !isObject(defs))
var extds = isDefs ? assigns : defs
if (isPlainObject(obj)) {
objectEach(obj, isDefs
? function (val, key) {
obj[key] = defs
}
: function (val, key) {
helperDeleteProperty(obj, key)
})
if (extds) {
assign(obj, extds)
}
} else if (isArray(obj)) {
if (isDefs) {
len = obj.length
while (len > 0) {
len--
obj[len] = defs
}
} else {
obj.length = 0
}
if (extds) {
helperFunctionApply(obj.push, obj, extds)
}
}
}
return obj
}
export default clear