Methods
# addEvent(target, eventName, eventHandler, ?useCapture)
兼容低版本 IE 的事件注册方法
Parameters:
Name | Type | Description |
---|---|---|
target |
HTMLElement
|
Window
|
事件源,window 或 DOM 元素 |
eventName |
String
|
事件名,如 load、click、mousedown |
eventHandler |
function
|
事件处理函数 |
?useCapture |
Boolean
|
是否使用事件捕获、默认值为 true |
Example
addEvent(window, 'hashchange', function(){
console.log('hash changed.');
};
# addHashEvent(callback)
监听页面哈希变化或页面历史变化
Parameters:
Name | Type | Description |
---|---|---|
callback |
*
|
页面哈希变化或页面历史变化时触发的回调函数 |
Example
addHashEvent(function(){
console.log('page changed');
})
# ajax(para)
发起一个 Ajax 请求
Parameters:
Name | Type | Description |
---|---|---|
para |
AjaxRequestArg
|
请求参数 |
Example
ajax({
url:'/example',
timeout:15000,
credentials:true,
cors:true,
type:'POST',
success:function (data) { console.log(data)},
error:function (data) { console.log(error)},
header:{ExtraHeader:'TestValue'},
data:{ name:'Alice', age:18 },
})
# base64Decode(str)
base64 解码,该方法会自动处理 Unicode 字符,
对等的应使用 base64Encode 方法进行编码
Parameters:
Name | Type | Description |
---|---|---|
str |
String
|
传入待解码字符串 |
解码后的字符串
Example
base64Decode('aGVsbG/kuJbnlYw=')//=> 'hello世界'
# base64Encode(str)
base64 编码码,该方法会自动处理 Unicode 字符,
对等的应使用 base64Decode 方法进行解码
Parameters:
Name | Type | Description |
---|---|---|
str |
*
|
传入待编码字符串 |
base64 编码后的字符串
Example
base64Encode('hello世界') //=> 'aGVsbG/kuJbnlYw='
# bindReady(fn, win)
监听页面加载完成,页面加载完成时执行回调。
通过
1. 监听 readystatechange 事件,在事件回调中检测当 readyState 值为 complete 时执行回调
2. 监听 DOMContentLoaded 事件,在事件回调中检测当 readyState 值为 complete 时执行回调
3. 监听 load 事件,在事件回调中检测当 readyState 值为 complete 时执行回调
三个监听任何一个达成回调值行逻辑后取消所有监听
页面加载完成时调用该方法,会立刻回调,并收到 lazy 回调值
1. 监听 readystatechange 事件,在事件回调中检测当 readyState 值为 complete 时执行回调
2. 监听 DOMContentLoaded 事件,在事件回调中检测当 readyState 值为 complete 时执行回调
3. 监听 load 事件,在事件回调中检测当 readyState 值为 complete 时执行回调
三个监听任何一个达成回调值行逻辑后取消所有监听
页面加载完成时调用该方法,会立刻回调,并收到 lazy 回调值
Parameters:
Name | Type | Description |
---|---|---|
fn |
bindReadyCallback
|
页面加载完成回调 |
win |
Window
|
指定 Window |
Example
bindReady(function()
{
console.log('page load complete')
});
# ConcurrentStorage(lockGetPrefix, lockSetPrefix) → {Undefined}
ConcurrentStorage 构造函数
Parameters:
Name | Type | Description |
---|---|---|
lockGetPrefix |
String
|
get 方法,锁前缀 |
lockSetPrefix |
String
|
set 方法,锁前缀 |
没有返回值
Undefined
Example
new ConcurrentStorage('123', '123') //=> undefined
# coverExtend(obj, ext)
使用源对象对目标对象进行扩展,
如果目标已经有该属性则不覆盖,如果没有的属性加进来
Parameters:
Name | Type | Description |
---|---|---|
obj |
Object
|
目标扩展对象 |
ext |
Object
|
源对象 |
扩展后的目标对象
Example
var a = {
name:'Alice',
age:18
}
var b = {
name: 'Bob',
favor: 'Apple'
}
coverExtend(a,b);
a //=> { name: "Alice",age: 18,favor: "Apple"}
b //=> { name:'Bob',favor:'Apple'}
# decodeURI(uri) → {String}
具备异常处理的 URI 解码方法
Parameters:
Name | Type | Description |
---|---|---|
uri |
String
|
传入的 uri 字符串 |
解码后的 uri,如果出现异常则返回原始传入值
String
Example
decodeURI('/hello%E4%B8%96%E7%95%8C') //=> '/hello世界'
# decodeURIComponent(uri) → {String}
具备异常处理的 URIComponent 解码方法
Parameters:
Name | Type | Description |
---|---|---|
uri |
String
|
传入的 uri 字符串 |
解码后的 uri,如果出现异常则返回原始传入值
String
Example
decodeURIComponent('%2Fhello%E4%B8%96%E7%95%8C') //=> 'hello世界'
# dfmapping(str)
对输入字符串进行乱序混淆,对混淆后的结果再次执行该方法则返回原来输入的值,
只支持大小写字母和数字,其他符号将不作处理
Parameters:
Name | Type | Description |
---|---|---|
str |
String
|
输入字符串 |
混淆后的值
Example
dfmapping('hello world') //=> 'zrkkm MmekV'
dfmapping('zrkkm MmekV') //=> 'hello world'
# each(obj, iterator, context)
对传入数组或对象的每个属性应用迭代器方法进行执行,
Parameters:
Name | Type | Description |
---|---|---|
obj |
Object
|
Array
|
传入对象 |
iterator |
iteratorCallback
|
迭代器方法 |
context |
Object
|
迭代器方法的执行上下文 |
Example
each([1,2,3],function(v,i,arr){console.log(v,i,arr)})
//1,0,[1, 2, 3]
//2,1,[1, 2, 3]
//3,2,[1, 2, 3]
# encodeDates(obj) → {String}
将传入对象中的所有 Date 类型的值转换为格式为 YYYY-MM-DD HH:MM:SS.sss
的字符串
Parameters:
Name | Type | Description |
---|---|---|
obj |
Object
|
传入的对象 |
传入对象,所有原有 Date 类型的值均已转换为 格式为 YYYY-MM-DD HH:MM:SS.sss 的字符串
String
Example
var v = encodeDates(
{
a:new Date('2020-02-02 8:0:12')
})
v //=> {a: '2020-02-02 08:00:12.00'}
# extend(obj, ext)
使用源对象对目标对象进行扩展,
只扩展第一层,
如果遇到目标对象已经存在的属性,则直接覆盖目标对象原来的属性值
如果遇到目标对象已经存在的属性,则直接覆盖目标对象原来的属性值
Parameters:
Name | Type | Description |
---|---|---|
obj |
Object
|
目标扩展对象 |
ext |
Object
|
源对象 |
扩展后的目标对象
Example
var a = {
name:'Alice',
age:18,
address:{
addr1: 'BeiJing'
}
}
var b = {
name: 'Bob',
favor: 'Apple',
address:{
addr1: 'TianJing'
}
}
extend(a,b);
a //=>
// {
// name: "Bob",
// age: 18,
// favor: "Apple",
// address:{
// addr1: 'TianJing'
// }
// }
# extend2Lev(obj, ext)
使用源对象对目标对象进行扩展,
允许扩展到第二级
Parameters:
Name | Type | Description |
---|---|---|
obj |
Object
|
目标扩展对象 |
ext |
Object
|
源对象 |
扩展后的目标对象
Example
var a = {
name:'Alice',
age:18,
address:{
addr1: 'BeiJing',
addr2: 'HeiBei'
}
}
var b = {
name: 'Bob',
favor: 'Apple',
address:{
addr1: 'TianJing'
}
}
extend2Lev(a,b);
a //=>
//{
// name: 'Bob',
// age: 18,
// favor: 'Apple',
// address:{
// addr1: 'TianJing'
// addr2: 'HeiBei'
// }
//}
# filter(arr, fn, context) → {Array}
使用指定过滤函数在指定源数组每一项上执行,返回一个新的数组包含指定过滤函数返回真值的数组项
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array
|
指定源数组 |
fn |
filterCallback
|
指定过滤函数 |
context |
Object
|
指定过滤函数执行上下文 |
新的数组,包含指定过滤函数值行返回真值的源数组项
Array
Example
filter([1,2,3,4,5,6],
function(v,i,arr)
{
console.log(v,i,arr);
return v>=4
});
//=>
// 1 0 [1, 2, 3, 4, 5, 6]
// 2 1 [1, 2, 3, 4, 5, 6]
// 3 2 [1, 2, 3, 4, 5, 6]
// 4 3 [1, 2, 3, 4, 5, 6]
// 5 4 [1, 2, 3, 4, 5, 6]
// 6 5 [1, 2, 3, 4, 5, 6]
// [4,5,6] // return value
# formatDate(date)
根据传入的 date 对象返回行如 YYYY-MM-DD HH:MM:SS.sss 的字符串,
如:'2020-02-02 20:20:02.20'
Parameters:
Name | Type | Description |
---|---|---|
date |
Date
|
传入的 date 对象 |
型如 YYYY-MM-DD:HH:MM:SS.ssssss 的字符串
Example
formatDate(new Date('2020-2-2 8:0:12')) //=> '2020-02-02 08:00:12.00'
# formatJsonString(obj)
指定两个空格作为缩进,对传入对象进行 JSON 字符串转换
Parameters:
Name | Type | Description |
---|---|---|
obj |
Object
|
传入对象 |
转换后的 JSON 字符串
Example
formatJsonString({a:1}) // => '{\n "a": 1\n}'
# getCookieTopLevelDomain(?hostname, ?testFlag) → {String}
获取指定域名的顶级域名, 例如在 a.example.com 中调用该方法,将返回 example.com
Parameters:
Name | Type | Description |
---|---|---|
?hostname |
String
|
指定域名,缺省值为当前域名 |
?testFlag |
String
|
指定 cookie 测试方法,获取顶层域名的原理是通过不断尝试在当前域名的上一层域名进行 cookie 读写测试, 来确定最终可以安全读写 cookie 的顶层域名,testFlag 为这个测试 cookie 的名字,如果不填写,将使用 domain_test 作为 testFlag |
指定域名的顶级域名
String
Example
// 在 www.example.com 域名下
getCookieTopLevelDomai() //=> example.com
# getDomBySelector(selector) → {Element}
通过选择器获取 dom 元素
Parameters:
Name | Type | Description |
---|---|---|
selector |
String
|
选择器 |
与选择器匹配的 dom 元素
Element
# getElementContent(element, tagName) → {String}
获取元素的文本内容
Parameters:
Name | Type | Description |
---|---|---|
element |
Element
|
dom 元素 |
tagName |
String
|
元素的标签名 |
元素文本内容
String
Example
var button = document.getElementById('btn1'); // <button id='btn1'>test</button>
getElementContent(button,'button'); //=> test
# getHostname(url, defaultValue)
获取指定 url 的域名
Parameters:
Name | Type | Description |
---|---|---|
url |
String
|
传入指定的 url |
defaultValue |
String
|
域名默认值,如果解析失败则返回该默认值 |
解析到的 url 的域名
Example
getHostname('https://www.example.com') //=> 'www.example.com'
# getIOSVersion() → {String}
通过调用 Navigator.appVersion 获取 ios 系统版本号
IOS 设备的系统版本号,如果获取失败则返回空字符串
String
# getQueryParam(url, key) → {String}
获取 url 中指定查询参数的值
Parameters:
Name | Type | Description |
---|---|---|
url |
String
|
传入 url |
key |
String
|
指定需要获取的查询参数的 key |
url 查询参数中指定 key 的值
String
Example
var val = getQueryParam('https://a.b.com?a=1&b=2','b');
console.log(val); // => 2
# getQueryParamsFromUrl(url) → {Object}
解析传入 url 中查询参数到一个含有查询参数列表的 key/value 对象
Parameters:
Name | Type | Description |
---|---|---|
url |
string
|
传入 url 字符串 |
一个含有参数列表的 key/value 对象
Object
Example
var url = _.getQueryParamsFromUrl('https://a.b.com?project=testproject&query1=test&silly=willy&field[0]=zero&field[2]=two#test=hash&chucky=cheese');
url.project; // => testproject
# getRandom() → {Number}
安全的 js 随机数生成方式,返回与原生 Math.random 类似的 0-1 的随机数值
一个介于 0 -1 的数字
Number
Example
getRandom() //=> 0.8368784293552812
# getRandomBasic(max)
获取指定数字范围内的随随机数
Parameters:
Name | Type | Description |
---|---|---|
max |
Number
|
随机数最大值 |
指定数字范围内的随机数
Example
getRandomBasic(100) //=> 85
# getSafeHostname(hostname) → {String}
Parameters:
Name | Type | Description |
---|---|---|
hostname |
String
|
传入 hostname,返回一个经过安全校验的 hostname |
String
# getScreenOrientation()
返回当前屏幕方向,可能值 ['未取到值', 'landscape', 'portrait']
经过以下测试:
IE 6 => '未取到值'
Opera 15 on macOS
Firefox 68 on macOS
Safari 12.1 on macOS
Chrome 75 on macOS
Safari on iPhone X
Chrome on Google Pixel 2
IE 6 => '未取到值'
Opera 15 on macOS
Firefox 68 on macOS
Safari 12.1 on macOS
Chrome 75 on macOS
Safari on iPhone X
Chrome on Google Pixel 2
屏幕方向,可能值 ['未取到值', 'landscape', 'portrait']
Example
getScreenOrientation() //=> 'landscape'
# getUA() → {BrowserInfo}
通过浏览器 UserAgent 获取当前浏览器型号和版本
浏览器型号和版本
Example
var browserInfo = getUA();
console.log(browserInfo); // => {chrome: 98}
# getURL(?url)
对传入的 url 字符串进行头尾空格去除,并进行 decodeURI 解码
若未传入 url 则对当前页面的地址进行 decodeURI 解码并返回
若未传入 url 则对当前页面的地址进行 decodeURI 解码并返回
Parameters:
Name | Type | Description |
---|---|---|
?url |
String
|
传入 url 字符串 |
返回解码后的 url 或 decodeURI 解码后的当前页面地址
Example
// 在 https://www.example.com
getURL() //=> https://www.example.com
# getURLPath(?url_path)
对传入的 url_path 字符串进行头尾空格去除,并进行 decodeURI 解码
若未传入 url_path 则对当前页面 URL 的路径部分进行 decodeURI 解码并返回
若未传入 url_path 则对当前页面 URL 的路径部分进行 decodeURI 解码并返回
Parameters:
Name | Type | Description |
---|---|---|
?url_path |
String
|
传入 url_path 字符串 |
返回解码后的 url_path 或 decodeURI 解码后的当前页面 URL 的路径部分
Example
// 在 "http://localhost:8080/世界.html"
getURLPath() //=> "/世界.html"
# getURLSearchParams(queryString) → {Object}
解析传入查询参数到一个含有查询参数列表的 key/value 对象
Parameters:
Name | Type | Description |
---|---|---|
queryString |
string
|
以问号开头的查询参数字符串 |
一个含有参数列表的 key/value 对象
Object
Example
var url = _.getURLSearchParams('?project=testproject&query1=test&silly=willy&field[0]=zero&field[2]=two#test=hash&chucky=cheese');
url.project; // => testproject
# hasAttribute(ele, attrName) → {Boolean}
检测是否具有指定属性名的属性
Parameters:
Name | Type | Description |
---|---|---|
ele |
Element
|
传入 dom 元素 |
attrName |
String
|
属性名 |
是否具有指定属性名的属性
Boolean
Example
var d = document.getElementById('sp1'); //<div id='sp1' test='123'></div>
hasAttribute(d,'test') //=> true
# hasAttributes(ele, attrNames)
检测传入 Dom 元素是否具有指定属性名数组中有至少一个属性
Parameters:
Name | Type | Description |
---|---|---|
ele |
Element
|
传入 Dom 元素 |
attrNames |
Array
|
传入属性名字符串数组 |
Dom 元素是否具有指定属性名数组中有至少一个属性
Example
var d = document.getElementById('sp1'); //<div id='sp1' test='123' test2='345'></div>
hasAttribute(d,['test']) //=> true
# hashCode(str)
对传入字符串做哈希计算,取值范围为 ±1E10
Parameters:
Name | Type | Description |
---|---|---|
str |
*
|
传入字符串 |
传入字符串的 hash 值
Example
hasdCode('hello world') //=> 1794106052
# hashCode53(str) → {Number}
对传入字符串进行 hash 计算,哈希值范围为 JS 可表示的安全值范围 ±9E15
Parameters:
Name | Type | Description |
---|---|---|
str |
String
|
传入字符串 |
传入字符串的哈希值
Number
Example
hashCode53('hello world') //=> -5975507814869267
# indexOf(arr, target)
在指定数组中查找目标对象的位置,若没有找到则返回 -1
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array
|
传入数组 |
target |
Object
|
查找目标 |
查找目标的下标
Example
indexOf([1,2,3,4],2) //=> 1
# inherit(subclass, superclass) → {function}
简单的原型链继承
Parameters:
Name | Type | Description |
---|---|---|
subclass |
function
|
子类构造函数 |
superclass |
function
|
父类构造函数 |
继承父类后的字类
function
Example
function A (){
this.say = function (arg){
console.log('say: ' + arg);
}
}
function B(){
this.sing = function (arg){
console.log('sing: ' + arg);
}
}
inherit(A,B);
var a =new A();
a.say('hello'); // say: hello
a.sing('hello'); // sing: hello
# isArguments(arg) → {Boolean}
检测是否是函数内部 arguments 对象
Parameters:
Name | Type | Description |
---|---|---|
arg |
*
|
传入参数 |
是否是函数内部 arguments 对象
Boolean
Example
(
function(){
var v = isArguments(arguments);
console.log(v) //=> true
}()
)
# isArray(arg) → {Boolean}
检测传入参数是否是数组类型
Parameters:
Name | Type | Description |
---|---|---|
arg |
*
|
传入参数 |
是否是数组类型
Boolean
Example
isArray([])//=> true
# isBoolean(arg) → {Boolean}
检测是否是布尔值
Parameters:
Name | Type | Description |
---|---|---|
arg |
*
|
传入参数 |
是否是布尔类型
Boolean
Example
isBoolean(true) //=> true
# isDate(arg) → {Boolean}
检测传入参数是否是日期对象
Parameters:
Name | Type | Description |
---|---|---|
arg |
*
|
传入参数 |
是否是日期类型
Boolean
Example
isDate(new Date()) //=> true
# isElement(arg) → {Boolean}
检测传入参数是否一个 Dom 元素
Parameters:
Name | Type | Description |
---|---|---|
arg |
*
|
传入参数 |
是否是 Dom 元素
Boolean
Example
var d = document.body;
isElement(d); //=> true
# isEmptyObject(arg) → {Boolean}
检测传入参数是否是空对象
Parameters:
Name | Type | Description |
---|---|---|
arg |
*
|
传入参数 |
是否是空对象
Boolean
Example
isEmptyObject({}) //=> true
# isFunction(arg)
检测传入参数是否是函数
Parameters:
Name | Type | Description |
---|---|---|
arg |
*
|
传入参数 |
是否是函数
Example
isFunction (function(){}) //=> true
# isHttpUrl(str) → {Boolean}
检测传入字符串是否是 http 或 https 地址
Parameters:
Name | Type | Description |
---|---|---|
str |
String
|
传入字符串 |
是否是 http 或 https 地址
Boolean
Example
isHttpUrl('https://www.example.com') //=> true
# isJSONString(arg) → {Boolean}
检测传入参数是否是合法 JSON 字符串
Parameters:
Name | Type | Description |
---|---|---|
arg |
String
|
传入字符串 |
是否是合法 JSON 字符串类型
Boolean
Example
isJSONString("{\"a\":123}") //=> true
# isNumber(arg) → {Boolean}
检测传入参数是否是数字
Parameters:
Name | Type | Description |
---|---|---|
arg |
*
|
传入参数 |
是否是数字类型
Boolean
Example
isNumber(1234) //=> true
# isObject(arg) → {Boolean}
检测传入参数是否是对象类型
Parameters:
Name | Type | Description |
---|---|---|
arg |
*
|
传入参数 |
是否是对象类型
Boolean
Example
isObject({}) //=> true
isObject(1) //=> false
# isString(arg) → {Boolean}
检测传入参数是否是字符串
Parameters:
Name | Type | Description |
---|---|---|
arg |
*
|
传入参数 |
是否是字符串
Boolean
Example
isString('1234') //=> true
# isSupportBeaconSend() → {Boolean}
检测是否支持 Beacon 数据发送
是否支持 Beacon 数据发送
Boolean
Example
// 再支持 beacon 的浏览器中
isSupportBeaconSend()//=> true
# isSupportCors() → {Boolean}
检测是否支持跨域的 ajax 数据发送
Boolean
Example
// 在支持跨域请求的浏览器中
isSupportCors()//=> true
# isUndefined(arg) → {Boolean}
检测传入参数是否等于 undefined
Parameters:
Name | Type | Description |
---|---|---|
arg |
*
|
传入参数 |
是否是 undefined 值
Boolean
Example
isUndefined(undefined) //=> true
isUndefined(null) //=> false
# jsonp(obj)
发起 jsonp 请求
Parameters:
Name | Type | Description |
---|---|---|
obj |
JsonpRequestArg
|
jsonp 请求参数体 |
Example
_.jsonp({
url:'https://example.com',
callbackName:'myDataCallback',
data:{name:'Alice'}, //服务端需要的其他参数,拼接在url后
success:function(data){console.log(data)},
error:function(err){console.error(err)},
timeout:3000
});
# listenPageState(obj)
监听页面状态变化,包括页面隐藏、显示、切换、获取焦点、丢失焦点
暴露 visible 和 hidden 回调,详细参见 listenPageStateArg
触发 visible 回调的时机: 获取焦点、或 visibilitychange 事件发生且当前页面可见
触发 hidden 回调的时机:丢失焦点、或 visibilitychange 事件发生且当前页面不可见
暴露 visible 和 hidden 回调,详细参见 listenPageStateArg
触发 visible 回调的时机: 获取焦点、或 visibilitychange 事件发生且当前页面可见
触发 hidden 回调的时机:丢失焦点、或 visibilitychange 事件发生且当前页面不可见
Parameters:
Name | Type | Description |
---|---|---|
obj |
listenPageStateArg
|
监听页面传参数、详细参见 listenPageStateArg |
Example
listenPageState({
visible:function(){
console.log('Page shows');
},
hidden:function(){
console.log('Page hides');
}
})
# loadScript(para)
加载 javascript 脚本或 css 脚本
Parameters:
Name | Type | Description |
---|---|---|
para |
loadScriptArg
|
加载脚本的参数,指定加载脚本类型及回调 |
Example
loadScript({
url:'/test.js',
type:'js',
success:function(){console.log('js script load succeed')}
})
# map(obj, iterator)
对传入数组的每个值执行映射方法,并返映射后值一个新的数组
Parameters:
Name | Type | Description |
---|---|---|
obj |
Array
|
传入对象 |
iterator |
iteratorCallback
|
迭代器映射方法 |
源数组每个元素执行映射方法后的的结果组成的新数组
Example
var v =map([1,2,3],function(v,i,arr){
console.log(v,i,arr);
return v+10;
})
// 1 0 [1, 2, 3]
// 2 1 [1, 2, 3]
// 3 2 [1, 2, 3]
v // [11,12,13]
# mediaQueriesSupported() → {Boolean}
检测是否支持媒体查询
是否支持媒体查询
Boolean
Example
// 支持媒体查询的浏览器中
mediaQueriesSupported()// => true
# now() → {Number}
获取当前时间相对于 1970-01-01 00:00:00 经过的毫秒数
返回当前时间相对于 1970-01-01 00:00:00 经过的毫秒数
Number
Example
now() // 1646122486530
# rot13defs(str) → {String}
对传入字符串进行 rot13 解密
Parameters:
Name | Type | Description |
---|---|---|
str |
String
|
传入待加密的字符串 |
rot13 解密后的字符串
String
Example
rot13defs('uryy|') //=> hello
# rot13obfs(str) → {String}
对传入字符串进行 rot13 加密
Parameters:
Name | Type | Description |
---|---|---|
str |
String
|
传入字符串 |
进行 rot13 加密后的字符串
String
Example
rot13obfs('hello') //=> 'uryy|'
# ry(dom) → {DomElementInfo}
Parameters:
Name | Type | Description |
---|---|---|
dom |
Element
|
传入的 dom 元素 |
元素信息对象,用于获取元素信息
Example
var a = document.getElementById('banner');
var b =ry(a);
b.addClass('banner-style');
// => <h1 id='banner' class='banner-style'> hello world </h1>
# safeJSONParse(str) → {Object}
对传入字符串进行安全的 JSON 反序列化操作,如果反序列化失败则返回 null
Parameters:
Name | Type | Description |
---|---|---|
str |
String
|
传入字符串 |
反序列化后的对象
Object
Example
safeJSONParse('{\"a\":124}') //=> {a: 124}
# searchObjDate(obj) → {String}
将传入对象中的所有 Date 类型的值转换为格式为 YYYY-MM-DD HH:MM:SS.sss
的字符串
Parameters:
Name | Type | Description |
---|---|---|
obj |
Object
|
传入对象 |
传入对象,所有原有 Date 类型的值均已转换为 格式为 YYYY-MM-DD HH:MM:SS.sss 的字符串
String
Example
var v = encodeDates(
{
a:new Date('2020-02-02 8:0:12')
})
v //=> {a: '2020-02-02 08:00:12.00'}
# setCssStyle(css)
创建 style 标签,填入传入 css 样式字符串
Parameters:
Name | Type | Description |
---|---|---|
css |
String
|
传入样式字符串 |
Example
setCssStyle(
`body
{
background :red
}
`)
// html head 中将插入
// <style>
// body
// {
// background:red
// }
// </style>
# strToUnicode(str) → {String}
将传入字符串转换为 unicode 编码
Parameters:
Name | Type | Description |
---|---|---|
str |
String
|
传入字符串 |
传入字符串的 unicode 编码
String
Example
strToUnicode('hello 世界') // => '\\68\\65\\6c\\6c\\6f\\20\\4e16\\754c'
# throttle(func, wait)
传入一个函数返回该函数的防抖函数
Parameters:
Name | Type | Description |
---|---|---|
func |
function
|
需要进行防抖的函数值行体 |
wait |
Number
|
防抖阈值,毫秒单位 |
传入函数的防抖函数
Example
function log(){
console.log('hello');
}
var throttleLog = throttle(log,1000);
setInterval(throttleLog,100);
// 每个间隔一秒打印一次 hello
hello
hello // 1s later
hello // 1s later
...
# toArray(iterable) → {Array}
将传入的对象或类数组转换为数组
Parameters:
Name | Type | Description |
---|---|---|
iterable |
Array
|
Object
|
传入的对象或类数组 |
包含对象或类数组的成员的数组
Array
Example
toArray({a:1,b:2})// =>[1, 2]
toArray([1,2]) // =>[1, 2]
# trim(str) → {String}
去除字符串开头和结尾的空白字符串
Parameters:
Name | Type | Description |
---|---|---|
str |
String
|
输入字符串 |
去除头尾空格后的结果
String
Example
const str = ' hello world ';
const val = trim (str); // val equals "hello world"
# unique(arr) → {Array}
对传入数组进行去重,返回新的数组
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array
|
传入数组参数 |
去重后的数组
Array
Example
var a = [1,1,2,3,3,4,5]
var b = unique(a);
b //=> [1,2,3,4,5,]
# URL(url) → {URL|URLObject}
兼容解析URL
如果浏览器原生支持 URL 类则返回原生 URL 对象
否则返回兼容实现的 URL 解析对象 ( 参见 URLObject)
如果浏览器原生支持 URL 类则返回原生 URL 对象
否则返回兼容实现的 URL 解析对象 ( 参见 URLObject)
Parameters:
Name | Type | Description |
---|---|---|
url |
String
|
url 格式的字符串 |
Example
var url = URL('http://www.domain.com:8080/path/index.html?project=testproject&query1=test&silly=willy&field[0]=zero&field[2]=two#test=hash&chucky=cheese');
url.hostname; // => www.domain.com
url.searchParams.get('project'); // => testproject
# urlParse(url) → {URLParser}
传入 URL 返回一个 URL 解析对象,用于添加查询参数,和重新获取添加查询参数后的 URL 字符串
Parameters:
Name | Type | Description |
---|---|---|
url |
String
|
传入需要添加查询参数的的 URL 字符串 |
一个 URL 解析对象,用于添加查询参数,和重新获取添加查询参数后的 URL 字符串
Example
let url = 'https://example.com'
let u = urlParse(url);
u.addQueryString({name:'Alice'});
u.getUrl(); // 'https://example.com?name=Alice'
# UUID() → {String}
浏览器环境的生成唯一 ID 的算法
唯一 ID
String
Example
UUID() //=> '17f44206897991-078fdaeab826c4c-37677a09-3686400-17f44206898caa'
# values(obj) → {Array}
将传入对象中所有属性的值通过一个数组返回
Parameters:
Name | Type | Description |
---|---|---|
obj |
*
|
传入对象 |
一个包含了传入对象所有属性值的数组
Array
Example
var a={
a:1,
b:2,
c:'hello'
}
var b = values (a)
b //=> [1,2,'hello']
# xhr(cors) → {ActiveXObject|XMLHttpRequest}
兼容低版本 IE 的 XMLHttpRequest 的实例化方法
Parameters:
Name | Type | Description |
---|---|---|
cors |
Boolean
|
请求是否需要支持跨域 |
XMLHttpRequest 的实例
ActiveXObject
|
XMLHttpRequest
Type Definitions
# ajaxErrorCallback(error, status)
Ajax 请求失败回调
Parameters:
Name | Type | Description |
---|---|---|
error |
Object
|
请求失败的异常结果 |
status |
Number
|
错误代码,如 404 |
Object
# AjaxRequestArg
Ajax 请求参数
Properties:
Name | Type | Description |
---|---|---|
url |
String
|
请求目标地址 |
timeout |
Number
|
请求超时时间,若超时请求将终止 |
credentials |
Boolean
|
标识是否携带 cookie |
cors |
Boolean
|
标识是否支持跨域 |
type |
String
|
标识请求类型,如 'GET','POST' |
success |
ajaxSuccessCallback
|
请求成功回调 |
error |
ajaxErrorCallback
|
请求失败的回调 |
header |
object
|
请求头,Key/Value 键值对对象 |
data |
Object
|
请求体,Key/Value 键值对对象 |
# bindReadyCallback(e)
Parameters:
Name | Type | Description |
---|---|---|
e |
Event
|
String
|
DOMContentLoaded 或 readystatechange 时间回调参数 当页面已经加载完成时,e 的值为 'lazy' 字符串 |
# BrowserInfo
Properties:
Name | Type | Description |
---|---|---|
?opera |
Number
|
欧朋版本号 |
?ie |
Number
|
IE 版本号 |
?edge |
Number
|
Edge 版本号 |
?firefox |
Number
|
Firefox 版本号 |
?chrome |
Number
|
Chrome 版本号 |
?safari |
Number
|
Safari 版本号 |
Object
# DomElementInfo
包含了 Dom 信息获取和设置方法的对象
Properties:
Name | Type | Description |
---|---|---|
addClass |
function
|
addClass (className:String)->void 为 Dom 元素添加样式类名 |
removeClass |
function
|
removeClass(className:String)->void 为 Dom 元素删除样式类名 |
hasClass |
function
|
hasClass(className:String)->Boolean 检测 Dom 元素是否具有指定样式类名 |
attr |
function
|
attr(key:String,?value:String)->String|null 获取和设置 Dom 元素属性。当只传 key 不传 value 时,方法获取元素中名为 key 的属性值。当传了 key 和 value 时,方法为 dom 元素设置名为 key 值为 value 的属性。 |
offset |
function
|
offset()->{left:Number,top:Number} 获取 Dom 元素相对浏览器窗口左上角的偏移位置 |
getSize |
function
|
getSize()->{width:NUmber, height:Number} 获取 Dom 元素的宽高 |
getStyle |
function
|
getStyle(property:String)->String 获取 Dom 元素的指定样式的值,如: getStyle('width') |
# filterCallback(value, index, sourceArray) → {Boolean}
Parameters:
Name | Type | Description |
---|---|---|
value |
Object
|
数组中的一项数据 |
index |
Number
|
该项数据的下标 |
sourceArray |
Array
|
源数组 |
是否通过校验,返回 true 则该数据项会被进入 filter 函数返回的新数组中,否则不会
Boolean
# iteratorCallback(value, index, sourceArray)
迭代器回调
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
当前迭代值 |
index |
Number
|
当前迭代值的下标 |
sourceArray |
Object
|
迭代源数组或对象 |
Object
# JsonpRequestArg
jsonp 请求参数
Properties:
Name | Type | Description |
---|---|---|
url |
String
|
请求地址 |
callbackName |
String
|
jsonp 数据回调函数,需要与服务端一致 |
data |
Object
|
服务端需要的其他参数,会拼接在 url 后 |
success |
jsonpSuccessCallback
|
请求成功回调函数 |
error |
jsonpErrorCallback
|
请求异常回调函数 |
timeout |
Number
|
超时时间 |
# listenPageStateArg
Properties:
Name | Type | Description |
---|---|---|
visible |
callback
|
页面从不见到可见回调 |
hidden |
callback
|
页面从可见到不可见对调 |
Object
# loadScriptArg
加载脚本参数
Properties:
Name | Type | Description |
---|---|---|
url |
String
|
脚本的网络地址 |
type |
String
|
脚本类型,可选值有 js、css |
success |
callback
|
脚本加载成功回调 |
error |
callback
|
脚本加载失败回调 |
# SearchParams
Properties:
Name | Type | Description |
---|---|---|
get |
function
|
get(key:String)->String 获取指定 key 的查询参数值 |
# URLObject
Properties:
Name | Type | Description |
---|---|---|
hash |
String
|
url 中的 hash 值 (#后的值) |
host |
String
|
url 中的主机地址 |
href |
String
|
url 完整链接 |
password |
String
|
url 中包含的主机账户密码 |
pathname |
String
|
url 中的路径名 |
port |
String
|
ulr 中的端口号 |
search |
String
|
url 中的查询参数 (?后的值) |
username |
String
|
url 中包含的主机用户名 |
hostname |
String
|
url 中的主机名 |
protocol |
String
|
url 的 协议,如 http: ,https |
origin |
String
|
url 的地址,只包含域名和端口 |
searchParams |
SearchParams
|
url 查询参数对象,可以通过其 get 方法获取指定的查询参数的值 |
Object
# URLParser
URL 解析器对象,用于添加查询参数,和重新获取添加查询参数后的 URL 字符串
Properties:
Name | Type | Description |
---|---|---|
setUrl |
function
|
setUrl(url:String)->void 重新设置需要解析的 url |
addQueryString |
function
|
addQueryString(obj:Object)->string 添加查询参数、传入参数是一个 Key/Value 键值对对象 |
getUrl |
function
|
getUrl()->string 重新获取 URL 字符串 |