8. 工具 -- Highway MVVM

来源:互联网 发布:csgom4a1皮肤知乎 编辑:程序博客网 时间:2024/06/03 22:54

Highway内置工具函数,可通过Highway.utils引用

8-1. unique

获取唯一标识

<!-- examples/tools/unique.html -->const id0 = Highway.utils.unique();const id1 = Highway.utils.unique('c');alert(`${id0}, ${id1}`);

8-2. assign

拓展对象

<!-- examples/tools/assign.html -->const obj0 = {  a: 'a',  b: 'b'};const obj1 = {  a: 'aa',  c: 'c'};const obj2 = Highway.utils.assign({}, obj0, obj1);alert(JSON.stringify(obj2));

8-3. include

返回数组中指定数据下标,如未找到,返回 -1

<!-- examples/tools/include.html -->const arr = [1, 2, 3, 4];const idx0 = Highway.utils.include(arr, 5);const idx1 = Highway.utils.include(arr, 2);alert(`${idx0},${idx1}`);

8-4. isPlainObject

是否为原生Object对象

<!-- examples/tools/isPlainObject.html -->alert(Highway.utils.isPlainObject({a: 'a'})); // truealert(Highway.utils.isPlainObject([0, 1])); // false

8-5. isDate

是否为日期

<!-- examples/tools/isDate.html -->alert(Highway.utils.isDate({})); // falsealert(Highway.utils.isDate(new Date)); // true

8-6. isObject

是否为对象

<!-- examples/tools/isObject.html -->alert(Highway.utils.isObject({})); // truealert(Highway.utils.isObject(new Date)); // truealert(Highway.utils.isObject(function () {})); // truealert(Highway.utils.isObject(1)); // false

8-7. isNumeric

是否为数字

<!-- examples/tools/isNumeric.html -->alert(Highway.utils.isNumeric(1)); // truealert(Highway.utils.isDate({})); // false

8-8. isTrue

是否为boolean true

<!-- examples/tools/isTrue.html -->alert(Highway.utils.isTrue(true)); // truealert(Highway.utils.isTrue('false')); // falsealert(Highway.utils.isTrue('1')); // true

false、’false’、”、’0’、null、undefined、0均被判断为boolean false,其他均被判断为true

8-9. MapList

映射列表

  • add(key, value)
  • find(key, value)
  • remove(key, value)
  • clear()
  • keys()
  • values()
<!-- examples/tools/MapList.html -->const mapList = new Highway.utils.MapList;mapList.add('a', '0');mapList.add('a', '1');mapList.add('a', '2');console.dir(mapList.find('a')); // ['0', '1', '2']mapList.remove('a', '1');console.dir(mapList.find('a')); // ['0', '2']mapList.add('b', '2');console.dir(mapList.keys()); // ['a', 'b']console.dir(mapList.values()); // ['0', '2', '2']mapList.clear();

8-10. secureHtml

安全HTML编码

““

console.log(Highway.utils.secureHtml(‘

8-10. secureUri

安全URI编码

<!-- examples/tools/secureUri.html -->// http://uri?q=11&amp;&lt;script&gt;alert(1)console.log(Highway.utils.secureHtml('http://uri?q=11&<script>alert(1)')); 

8-11. getAttrs

获取DOM元素所有属性

<div id="attr" directive-0:attr="exp" style="backgrond-color:red;"></div>//{"id":"attr","directive-0:attr":"exp","style":"backgrond-color:red;"}console.dir(Highway.utils.getAttrs($('#attr')));
0 0
原创粉丝点击