创建全局对象:_
Members
-
static, readonly DEFAULT_SCALE
-
默认数值保留小数位数
-
static, readonly EPSILON
-
精度范围
-
static, readonly VERSION :string
-
当前版本号
- Default Value:
- 1.8.3
Methods
-
static cl(num, opration) → {string}
-
阿拉伯数字转中文数字 (源码:https://github.com/cnwhy/nzh.git)
Parameters:
Name Type Description num
String 阿拉伯数字/字符串 , 科学记数法字符串 opration
Object 转换配置
{
ww: {万万化单位 | false}
tenMin: {十的口语化 | false}
nzh:{中文语言|简体}
}Returns:
string- Version:
- 1.8.3
Example
_.cl("100111",{tenMin:true}) => "十万零一百一十一" _.cl("100111") => "一十万零一百一十一" _.cl("13.5",{tenMin:true}) => "十三点五" _.cl(1e16) => "一亿亿"
-
static clone(obj) → {*|_|*}
-
创建 一个浅复制(浅拷贝)的克隆object。任何嵌套的对象或数组都通过引用拷贝,不会复制。
Parameters:
Name Type Description obj
Object 要克隆的对象 Returns:
* | _ | *Example
_.clone({name: 'moe'}); => {name: 'moe'};
-
static copyToClipboard()
-
复制文本到剪切板(适用于Chrome、Firefox、Internet Explorer和Edge,以及Safari)
-
static dateFormat(date, format) → {*}
-
将 Date 转化为指定格式的String 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q)
可以用 1-2 个占位符 年(y)可以用 1-4 个占位符 毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
Parameters:
Name Type Description date
Date 日期(默认当前时间) format
String 格式化字符串(默认yyyy-MM-dd hh:mm:ss) Returns:
*Example
_.dateFormat(date,"yyyy-MM-dd hh:mm:ss.S") ==> 2016-05-04 08:09:04.423 _.dateFormat(date,"yyyy-M-d h:m:s.S") ==>2016-05-04 8:9:4.18
-
static debounce(fn, wait, immediate)
-
函数去抖,空闲时间大于或等于wait,执行fn
Parameters:
Name Type Description fn
function 要调用的函数 wait
Integer 延迟时间(单位毫秒) immediate
Boolean 给immediate参数传递false绑定的函数先执行,而不是wait之后执行 Returns:
- 实际调用函数Example
var lazyLayout = _.debounce(calculateLayout, 300); $(window).resize(lazyLayout);
-
static defineColor(value, color) → {string}
-
定义文字颜色
Parameters:
Name Type Description value
String 原始文字 color
String 要定义的颜色(默认红色) Returns:
stringExample
_.defineColor(“Hello”) =><span style="color:#FF0000">Hello</span>
-
static defineOperate(arr, value) → {string}
-
定义操作列
Parameters:
Name Type Description arr
Array 一个数组 value
Integer 对应行的id值 Returns:
string - 返回html字符串Example
var arr = [ { text: "删除", fn: "detailDataGrid.Delete({0})" }, {text: "修改", fn: "detailDataGrid.Edit({0})" }] _.defineOperate(arr,3) =><a style='cursor: pointer;' onclick='detailDataGrid.Delete(3)' href='javascript:;'>删除</a> | <a style='cursor: pointer;' onclick='detailDataGrid.Edit(3)' href='javascript:;'>修改</a>
-
static difference(a, b) → {*}
-
数组差集,以第一个数组为主
Parameters:
Name Type Description a
Array 主数组 b
Array 次数组 Returns:
*Example
var a = [1,2,3,4,5] var b = [2,4,6,8,10] _.difference(a, b) =>[1,3,5]
-
static each(obj, iteratee, context)
-
遍历list中的所有元素,按顺序用遍历输出每个元素。
如果传递了context参数,则把iteratee绑定到context对象上。
每次调用iteratee都会传递三个参数:(element, index, list)。
如果list是个JavaScript对象,iteratee的参数是 (value, key, list))。
返回list以方便链式调用。
Parameters:
Name Type Description obj
Object 遍历目标 iteratee
function 迭代器 context
Object 绑定的目标对象 Example
_.each([1, 2, 3], alert); => 依次提示每个数字... _.each({one: 1, two: 2, three: 3}, alert); => 依次提示每个数字...
-
static equals(a, b, strict) → {boolean}
-
判断数组是否相等,默认为严格模式比较
Parameters:
Name Type Description a
Array 第一个数组 b
Array 第二个数组 strict
Boolean 是否严格比较(默认为true) Returns:
booleanExample
var arr1 = [1, 2, 3, 4]; var arr2 = [2, 1, 4, 3]; var arr3 = [2, 2, 3, 4]; var arr4 = [1, 2, 3, 4]; _.equals(arr1,arr2); => false _.equals(arr1,arr2, false); =>true _.equals(arr1,arr3); =>false _.equals(arr1,arr3, false); =>false _.equals(arr1,arr4); =>true _.equals(arr1,arr4, false); =>true
-
static every(list, predicate, context) → {boolean}
-
如果list中的所有元素都通过predicate的真值检测就返回true。
(注:如果存在原生的every方法,就使用原生的every。)
predicate 通过 iteratee 进行转换,以简化速记语法。Parameters:
Name Type Description list
Array 要检查的列表 predicate
function 检测函数 context
Object 上下文 Returns:
booleanExample
_.every([2, 4, 5], function(num) { return num % 2 == 0; }); => false
-
static extend(obj) → {_}
-
允许自己的实用程序函数扩展jPublic。传递一个 {name: function}定义的哈希添加到jPublic对象,以及面向对象封装。
Parameters:
Name Type Description obj
Returns:
_Example
_.extend({ abc: function(str) { return str; } }); _.abc("Hello"); =>"Hello"
-
static extendOwn()
-
类似于 extend, 但只复制自己的属性覆盖到目标对象。(注:不包括继承过来的属性)。
Example
var a = { foo: false }; var b = { bar: true }; _.extendOwn(a,b) =>{ foo: false, bar: true };
-
static fmoney(value, digit) → {string}
-
金额格式化
Parameters:
Name Type Description value
Number 原始金额数值 digit
Integer 保留小数位置(默认2位) Returns:
stringExample
_.fmoney(100000000) =>100,000,000.00 _.fmoney(100000000.3434343, 3) =>100,000,000.343
-
static format(format) → {string|void|string}
-
格式化字符串
Parameters:
Name Type Description format
String 要格式化的字符串 Returns:
- string | void
- string
Example
_.format("Hello, {0}!","World")=>Hello, World _.format("Hello, {0}, My {1}!","World","Love You")=>Hello, World, My Love You
-
static formatBytes(bytes, decimals) → {string}
-
字节格式化
Parameters:
Name Type Description bytes
Number 字节值 decimals
Integer 小数位数 Returns:
stringExample
formatBytes(1024); =>1 KB formatBytes('1024'); =>1 KB formatBytes(1234); =>1.21 KB formatBytes(1234, 3); =>1.205 KB
-
static formatTime(n) → {string}
-
将时间格式化为指定格式的String
Parameters:
Name Type Description n
Number 时间(单位秒) Returns:
stringExample
_.formatTime(25); => '0:00:25' _.formatTime(63846); => '17:44:06'
-
static functions(obj) → {this}
-
返回一个对象里所有的方法名, 而且是已经排序的 — 也就是说, 对象里每个方法(属性值是一个函数)的名称.
Parameters:
Name Type Description obj
Object 查找对象 Returns:
thisExample
_.functions(_); => ["arrayDiff", "arrayEquals", "arrayIsRepeat", "clone", "debounce", "defineColor" ...
-
static getFormJson(frm)
-
获取表单数据
Parameters:
Name Type Description frm
Object 表单对象 Example
获得表单数据,自动拼接成json对象,提交给服务端 $.ajax({ type: 'post', url: 'your url', data: _.getFormJson($('#formId')), success: function(data) { // your code } });
-
static getRandom(min, max) → {number}
-
返回一个min 和 max之间的随机整数。
如果你只传递一个参数,那么将返回0和这个参数之间的整数Parameters:
Name Type Description min
随机数下限,没传默认为0 max
随机数上限 Returns:
numberExample
_.random(0, 100); => 48
-
static getRandomDate(begin, end) → {Date}
-
随机获取一个日期
Parameters:
Name Type Description begin
Date 开始时间(默认1970-1-1) end
Date 结束时间(默认当前时间) Returns:
Date- Version:
- 1.8.3
Example
_.dateFormat(_.getRandomDate()); =>2019-05-01 12:34:54
-
static getRootPath() → {string}
-
获取当前网站根路径
Returns:
stringExample
http://localhost:8083/tqlin
-
static getUrlParam(name) → {string|null}
-
获得当前Url参数值
Parameters:
Name Type Description name
String 参数名 Returns:
string | null -
static getWeekEndDate(date) → {Date}
-
获得本周的结束日期
Parameters:
Name Type Description date
Date 日期(默认当前日期) Returns:
DateExample
_.dateFormat(_.getWeekEndDate()); =>2019-05-13 00:00:00
-
static getWeekStartDate(date) → {Date}
-
获得本周的开始日期
Parameters:
Name Type Description date
Date 日期(默认当前日期) Returns:
DateExample
_.dateFormat(_.getWeekStartDate()); =>2019-05-13 00:00:00
-
static isArray(obj) → {boolean}
-
如果obj是一个数组,返回true。
Parameters:
Name Type Description obj
Object 要检查的对象 Returns:
booleanExample
(function(){ return _.isArray(arguments); })(); => false _.isArray([1,2,3]); => true
-
static isFunction(obj) → {boolean}
-
如果obj是一个函数(Function),返回true。
Parameters:
Name Type Description obj
Object 要检查的对象 Returns:
booleanExample
_.isFunction(alert); => true
-
static isMatch(object, attrs)
-
判断properties中的键和值是否包含在object中。
Parameters:
Name Type Description object
Object 查找目标 attrs
Object 查找对象 Example
var stooge = {name: 'moe', age: 32}; _.isMatch(stooge, {age: 32}); => true
-
static isNullOrEmpty(str) → {boolean}
-
是否为空字符串
Parameters:
Name Type Description str
String 要检查的字符串 Returns:
booleanExample
_.isNullOrEmpty(" "); =>true .isNullOrEmpty(' '); =>true var student = {className: "测试班", name: "我是张三", age: 18}; _.isNullOrEmpty(student.skill); =>true _.isNullOrEmpty(undefined); =>true _.isNullOrEmpty(null); =>true _.isNullOrEmpty(""); =>true _.isNullOrEmpty('') =>true
-
static isNumeric(value) → {boolean}
-
是否数值
Parameters:
Name Type Description value
Object 要检查的对象 Returns:
booleanExample
// true _.isNumeric( "-10" ) _.isNumeric( "0" ) _.isNumeric( 0xFF ) _.isNumeric( "0xFF" ) _.isNumeric( "8e5" ) _.isNumeric( "3.1415" ) _.isNumeric( +10 ) _.isNumeric( 0144 ) // false _.isNumeric( "-0x42" ) _.isNumeric( "7.2acdgs" ) _.isNumeric( "" ) _.isNumeric( {} ) _.isNumeric( NaN ) _.isNumeric( null ) _.isNumeric( true ) _.isNumeric( Infinity ) _.isNumeric( undefined )
-
static isObject(obj) → {boolean}
-
如果object是一个对象,返回true。需要注意的是JavaScript数组和函数是对象,字符串和数字不是。
Parameters:
Name Type Description obj
Object 要检查的对象 Returns:
booleanExample
_.isObject({}); => true _.isObject(1); => false
-
static isRepeat(arr) → {boolean}
-
数组元素是否重复
Parameters:
Name Type Description arr
Array 要检查的数组对象 Returns:
booleanExample
var a = [1, 2, 3, 4, 5]; var b = [1, 2, 3, 5, 5]; _.isRepeat(a); =>false _.isRepeat(b); =>true
-
static isString(value) → {boolean}
-
如果object是一个字符串,返回true。
Parameters:
Name Type Description value
String 要检查的值 Returns:
booleanExample
_.isString("moe"); => true
-
static isValidDate(date) → {boolean}
-
是否为日期对象,参数支持Date和String类型
Parameters:
Name Type Description date
Object 要检查的对象 Returns:
boolean- Version:
- 1.8.3
Example
_.isValidDate("2019-5-5a"); =>false _.isValidDate("2019-5-5"); =>true _.isValidDate(new Date("2019-5-5a")); =>false
-
static iteratee(value, context)
-
生成可应用于集合中的每个元素的回调。
_.iteratee支持许多常见回调用例的简写语法。
根据值的类型,_.iteratee 各种结果Parameters:
Name Type Description value
* 迭代值 context
Object 上下文 Example
// 空值 _.iteratee(); => _.identity() // 函数 _.iteratee(function(n) { return n * 2; }); => function(n) { return n * 2; } // 对象 _.iteratee({firstName: 'Chelsea'}); => _.matcher({firstName: 'Chelsea'}); // 其它 _.iteratee('firstName'); => _.property('firstName');
-
static keys(obj) → {Array|*}
-
检索object拥有的所有可枚举属性的名称。
Parameters:
Name Type Description obj
Object 要检索的对象 Returns:
Array | *Example
_.keys({one: 1, two: 2, three: 3}); => ["one", "two", "three"]
-
static lastDay(date) → {Date}
-
获取月份最后一天
Parameters:
Name Type Description date
Date 日期(默认当前日期) Returns:
DateExample
_.dateFormat(_.lastDay()); =>2019-05-31 00:00:00
-
static ltrim(str, chars) → {string}
-
去左空格
Parameters:
Name Type Description str
String 字符串 chars
String 要移除的字符(默认为空白字符) Returns:
stringExample
_.ltrim(" Hello ")=>"Hello " _.ltrim("_Hello_","_")=>"Hello_"
-
static matcher(attrs)
-
返回一个断言函数,这个函数会给你一个断言可以用来辨别给定的对象是否匹配attrs指定键/值属性。
Parameters:
Name Type Description attrs
Example
var ready = _.matcher({selected: true, visible: true}); var readyToGoList = _.filter(list, ready);
-
static now()
-
获取当前时间戳,兼容旧环境(毫秒)
Example
_.now() =>521557891109615
-
static numAdd(x, y) → {number}
-
加
Parameters:
Name Type Description x
* 第一个数值 y
* 第二个数组 Returns:
numberExample
_.numAdd('3',5); =>8
-
static numCompare(x, y) → {number}
-
比较两数大小,x>y返回1,x==y返回0,否则返回-1
Parameters:
Name Type Description x
Number 第一个数值 y
Number 第二个数值 Returns:
numberExample
_.numCompare(3,5); =>-4 -.numCompare('aaa',3); =>抛出异常Parameter is not a number!
-
static numDivide(x, y) → {number}
-
除
Parameters:
Name Type Description x
* 第一个数值 y
* 第二个数值 Returns:
numberExample
_.numDivide(9,'3'); =>3
-
static numEqual(x, y) → {boolean}
-
判断两个小数数值是否相等
Parameters:
Name Type Description x
Number 第一个数值 y
Number 第二个数值 Returns:
booleanExample
_.numEqual(0.1+0.2, 0.3); =>true
-
static numMultiply(x, y) → {number}
-
乘
Parameters:
Name Type Description x
* 第一个数值 y
* 第二个数值 Returns:
numberExample
_.numMultiply('2',3); =>6
-
static numSubtract(x, y) → {number}
-
减
Parameters:
Name Type Description x
* 第一个数值 y
* 第二个数值 Returns:
numberExample
_.numSubtract('7',5); =>2
-
static poll(fn, callback, errback, timeout, interval)
-
轮询条件函数,根据状态执行相应回调
Parameters:
Name Type Description fn
function 条件函数 callback
function 成功回调 errback
function 失败回调 timeout
Integer 超时间隔(毫秒) interval
Integer 轮询间隔(毫秒) Example
确保元素可见 poll( function () { return document.getElementById('lightbox').offsetWidth > 0; }, function () { // Done, success callback }, function () { // Error, failure callback } );
-
static range(start, stop, step) → {Array.<any>}
-
一个用来创建整数灵活编号的列表的函数,便于each 和 map循环。
如果省略start则默认为 0;
step 默认为 1.返回一个从start 到stop的整数的列表,用step来增加 (或减少)独占。
值得注意的是,如果stop值在start前面(也就是stop值小于start值),那么值域会被认为是零长度,而不是负增长。-如果你要一个负数的值域 ,请使用负数step.Parameters:
Name Type Description start
Integer 开始位置 stop
Integer 结束位置 step
Integer 步长 Returns:
Array.<any>Example
_.range(10); => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] _.range(1, 11); => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] _.range(0, 30, 5); => [0, 5, 10, 15, 20, 25] _.range(0, -10, -1); => [0, -1, -2, -3, -4, -5, -6, -7, -8, -9] _.range(0); => []
-
static rtrim(str, chars) → {string}
-
去右空格
Parameters:
Name Type Description str
字符串 chars
要移除的字符(默认为空白字符) Returns:
stringExample
_.ltrim(" Hello ")=>" Hello" _.ltrim("_Hello_","_")=>"_Hello"
-
static runOnce(fn, context) → {function}
-
函数只执行一次
Parameters:
Name Type Description fn
function 要执行的函数 context
Object 上下文 Returns:
functionExample
var a = 0; var canOnlyFireOnce = runOnce(function () { a++; console.log(a); }); canOnlyFireOnce(); =>1 canOnlyFireOnce(); => nothing canOnlyFireOnce(); => nothing
-
static serverTime() → {Date}
-
获取当前服务器时间(Date)
Returns:
DateExample
_.serverTime() =>Wed May 15 2019 11:33:22 GMT+0800 (中国标准时间)
-
static shortDateFormat(date)
-
返回日期的yyyy-MM-dd格式
Parameters:
Name Type Description date
日期 Example
_.shortDateFormat(_.now()); =>2019-05-15
-
static shuffle(array) → {Array}
-
洗牌数组
Parameters:
Name Type Description array
String 要洗牌的数组 Returns:
ArrayExample
var a = [1,2,3,4,5] _.shuffle(a) => [3, 2, 4, 5, 1]
-
static some(list, predicate, context) → {boolean}
-
如果list中有任何一个元素通过 predicate 的真值检测就返回true。
一旦找到了符合条件的元素, 就直接中断对list的遍历。
predicate 通过 iteratee 进行转换,以简化速记语法。Parameters:
Name Type Description list
Array 要检查的列表 predicate
function 检测函数 context
Object 上下文 Returns:
booleanExample
_.some([null, 0, 'yes', false]); => true
-
static throttle(fn, wait, scope) → {function}
-
函数节流 每wait时间间隔,执行fn
Parameters:
Name Type Description fn
function 要调用的函数 wait
Integer 延迟时间,单位毫秒 scope
Object scope代替fn里this的对象 Returns:
function - 实际调用函数Example
var throttled = _.throttle(updatePosition, 100); $(window).scroll(throttled);
-
static toFixed(x, digits) → {string|*}
-
固定小数位数
Parameters:
Name Type Description x
Number 要处理的数值 digits
Number 小数位置,默认保留2位 Returns:
string | *Example
_.toFixed(548.6512) =>548.65
-
static toFixed(x) → {string}
-
移除数值指数表示
Parameters:
Name Type Description x
Number 要处理的数值 Returns:
stringExample
var numbers = [ 1.1234567890123456789e+30, 1.1234567890123456789e-30, -1.1234567890123456789e+30, -1.1234567890123456789e-30] var i; for (i=0;i<numbers.length;i++) { console.log(_.removeExponent(numbers[i])); } =>1123456789012345700000000000000 =>0.0000000000000000000000000000011234567890123458 =>-1123456789012345700000000000000 =>0.00000000000000000000000000000.11234567890123458
-
static trim(str, chars) → {string}
-
去左右空格
Parameters:
Name Type Description str
String 字符串 chars
String 要移除的字符(默认为空白字符) Returns:
stringExample
_.trim(" Hello ")=>"Hello" _.trim("_Hello_","_")=>"Hello"
-
static truncate(str, limit, suffix) → {string|*}
-
截取字符串
Parameters:
Name Type Description str
String 原始字符串 limit
Integer 长度限制(默认限制长度100) suffix
String 超过替换字符(默认用'...'替代) Returns:
string | *Example
_.truncate('We are doing JS string exercises.') =>We are doing JS string exercises. _.truncate('We are doing JS string exercises.',19) =>We are doing JS ... _.truncate('We are doing JS string exercises.',15,'!!') =>We are doing !!
-
static unformatTime(string) → {number}
-
反格式化,与formatTime函数相反
Parameters:
Name Type Description string
String 要格式化的时间字符串 Returns:
numberExample
_.unformatTime('0:00:25'); => 25 _.unformatTime('17:44:06'); => 63846
-
static unique(arr) → {Array}
-
数组去重
Parameters:
Name Type Description arr
Array 要去重的数组 Returns:
ArrayExample
var b = [1, 2, 3, 5, 5]; _.unique(b); => [1, 2, 3, 5]