Class: Utils
优质
小牛编辑
128浏览
2023-12-01
THING. Utils
Utils 工具辅助类
Utils()
Methods
<static> cloneObject(obj [, shallow]) → {Object}
克隆 js 对象
Parameters:
Name Type Argument Description obj
Object js 对象
shallow
Boolean <optional> 是否进行浅拷贝处理,默认 true
Returns:
- Type
- Object
<static> convertObjectClass(object, className)
转换物体对象类型
Parameters:
Name Type Description object
THING.BaseObject 物体对象
className
String 目标类名称
Example
var bus = THING.Utils.convertObjectClass(obj, 'Bus');// 批量转换var things = app.query('.Thing');things.forEach(function(obj){ THING.Utils.convertObjectClass(obj, 'Bus');})
<static> debug()
输出 debug 日志
<static> dynamicLoad(urls, callback [, timestamp] [, inOrder]) → {Object}
异步加载 css 文件、js 文件、json 文件(需支持跨域访问)
Parameters:
Name Type Argument Description urls
String | Array.<String> 文件路径
callback
function 加载完成后回调方法
timestamp
Boolean <optional> 选填参数,url 后面是否添加时间戳以便每次访问时清除缓存,默认 ture
inOrder
Boolean <optional> 选填参数,文件是否按数组中的顺序加载,默认 true
Returns:
- Type
- Object
Example
THING.Utils.dynamicLoad([ "https://thingjs.com/example.js", "https://thingjs.com/example.css", "https://thingjs.com/example.json"], function(result){ THING.Utils.log('Loading complete!'); }, true, // 选填 是否带时间戳 true // 选填 是否按顺序下载)
<static> error()
输出错误日志
<static> generateUUID() → {String}
生成唯一的 UUID
Returns:
- Type
- String
Example
// 返回如 7E5D4533-C059-40D3-8D19-0566ACD03CE3THING.Utils.log(THING.Utils.generateUUID())
<static> isBlank(o) → {Boolean}
判断传入的值是否为空白字符串
Parameters:
Name Type Description o
* js 元素
Returns:
- Type
- Boolean
Example
THING.Utils.log(THING.Utils.isBlank(' ')) // true
<static> isBoolean(value) → {Boolean}
判断传入的值是为否布尔类型
Parameters:
Name Type Description value
* js 元素
Returns:
- Type
- Boolean
<static> isDom(value) → {Boolean}
判断传入的值是否为 DOM 元素
Parameters:
Name Type Description value
* js 元素
Returns:
- Type
- Boolean
<static> isEmptyArray(o) → {Boolean}
判断传入的值是为空数组
Parameters:
Name Type Description o
* js 元素
Returns:
- Type
- Boolean
Example
var arr=[];THING.Utils.log(THING.Utils.isEmptyArray(arr)) // true
<static> isEmptyObj(o) → {Boolean}
判断传入的值是否为空对象
Parameters:
Name Type Description o
* js 元素
Returns:
- Type
- Boolean
Example
var obj={};THING.Utils.log(THING.Utils.isEmptyObj(obj)) // true
<static> isEqual(o1, o2) → {Boolean}
判断对象是否完全相等
Parameters:
Name Type Description o1
Object 第一个对象
o2
Object 第二个对象
Returns:
- Type
- Boolean
Example
var obj1={'name':'Thingjs'};var obj2={'name':'thingjs'};THING.Utils.log(THING.Utils.isEqual(obj1,obj2)) // false
<static> isNull(o) → {Boolean}
判断传入的值是否为空( null 或 undefined )
Parameters:
Name Type Description o
* js 元素
Returns:
- Type
- Boolean
<static> log()
输出日志
<static> mergeObject(target, source [, overwrite]) → {Object}
合并简单对象 将源对象的 属性/属性值 合并至目标对象
Parameters:
Name Type Argument Description target
Object 目标对象
source
Object 源对象
overwrite
Boolean <optional> 是否更新已经存在的属性,默认 false
Returns:
- Type
- Object
Example
var obj1 = { 'name': 'ThingJS', 'year': '2018' };var obj2 = { 'version': 'xxx', 'year': '2019' };// {"name":"ThingJS","year":"2018","version":"xxx"}THING.Utils.log(THING.Utils.mergeObject(obj1,obj2))// {"name":"ThingJS","year":"2019","version":"xxx"}THING.Utils.log(THING.Utils.mergeObject(obj1,obj2,true))
<static> objectKeysToLowerCase(input [, deep] [, filter]) → {Object}
将对象的成员键值全部转换成小写
Parameters:
Name Type Argument Description input
Object 要处理的 js 对象
deep
Boolean <optional> 是否递归转换所有的键值,默认 false
filter
function <optional> 键值过滤函数
Returns:
- Type
- Object
Example
var obj = { 'Name': 'ThingJS', 'Info': { 'Year': 'Hello 2019','Verson':'xxxx' } };// 默认不转换嵌套的 key 值// 返回 {"name":"ThingJS","info":{"Year":"Hello 2019","Verson":"xxxx"}}THING.Utils.log(THING.Utils.objectKeysToLowerCase(obj))// 递归转换所有键值// {"name":"ThingJS","info":{"year":"Hello 2019","verson":"xxxx"}}THING.Utils.log(THING.Utils.objectKeysToLowerCase(obj,true))// filter 函数过滤,返回 false 时不转换// {"name":"ThingJS","info":{"Year":"Hello 2019","verson":"xxxx"}}THING.Utils.log(THING.Utils.objectKeysToLowerCase(obj, true, function (key) { if (key == 'Year') { return false; } else { return true; }}))
<static> toLowerCase(s) → {String}
字符串转成小写
Parameters:
Name Type Description s
String 字符串
Returns:
- Type
- String
Example
THING.Utils.log(THING.Utils.toLowerCase('ThingJS')) // thingjs
<static> warn()
输出警告日志