tnblog
首页
视频
资源
登录

拓展js通用方法来对应c#操作

4488人阅读 2018/12/14 20:12 总访问:29935 评论:0 收藏:0 手机
分类: js


有很多时候,js不能像c#一样有那样多的方法操作,但是我们可以自己写一些方法,让他拥有:

比如最常见的一些:

注意:js里的注释最好用/**/,便于压缩

  1. /*isNumber*/
  2. /*功能:是否是数字*/
  3. String.prototype.isNumber = function ({
  4.     return !isNaN(parseFloat(this)) && isFinite(this);
  5. }
  6. /*format*/
  7. /*功能:你懂的*/
  8. String.prototype.format = function ({
  9.     if (arguments.length == 0return this;
  10.     for (var s = this, i = 0; i < arguments.length; i++)
  11.         s = s.replace(new RegExp("\\{" + i + "\\}""g"), arguments[i]);
  12.     return s;
  13. };
  14. /*功能:去空格*/
  15. String.prototype.trim = function ({
  16.     return this.replace(/(^\s*)|(\s*$)/g"");
  17. }
  18. /*获取路径参数*/
  19. JSON.parseQueryString = function (url{
  20.     var reg_url = /^[^\?]+\?([\w\W]+)$/,
  21.      reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
  22.      arr_url = reg_url.exec(url),
  23.      ret = {};
  24.     if (arr_url && arr_url[1]) {
  25.         var str_para = arr_url[1], result;
  26.         while ((result = reg_para.exec(str_para)) != null) {
  27.             ret[result[1]] = result[2];
  28.         }
  29.     }
  30.     return ret;
  31. };
  32. /*格式化金钱格式*/
  33. Number.prototype.formatMoney = function (places, symbol, thousand, decimal{
  34.     places = !isNaN(places = Math.abs(places)) ? places : 2;
  35.     symbol = symbol !== undefined ? symbol : "¥";
  36.     thousand = thousand || ",";
  37.     decimal = decimal || ".";
  38.     var number = this,
  39.         negative = number < 0 ? "-" : "",
  40.         i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "",
  41.         j = (j = i.length) > 3 ? j % 3 : 0;
  42.     return symbol + negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g"$1" + thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : "");
  43. };
  44. /*删除下标数组*/
  45. Array.prototype.remove = function (dx{
  46.     if (isNaN(dx) || dx > this.length) { return false; }
  47.     for (var i = 0, n = 0; i < this.length; i++) {
  48.         if (this[i] != this[dx]) {
  49.             this[n++] = this[i]
  50.         }
  51.     }
  52.     this.length -= 1
  53. };
  54. /*无视大小写取值json对应值*/
  55. var getKeyValue = function (obj, key, ignore{
  56.     if (typeof (obj) != "object" || key==undefinedreturn;
  57.     if (ignore == undefined) ignore = true;
  58.    
  59.     var _res = null;
  60.     $.each(obj, function (k, v{
  61.         if (k != undefined && k != null&& k.toString().toLowerCase() == key.toLowerCase()) _res = v;
  62.     });
  63.     return _res;
  64. };
  65. //数组制定排序
  66. Array.prototype.OrderByAsc = function (func{
  67.     var m = {};
  68.     for (var i = 0; i < this.length; i++) {
  69.         for (var k = 0; k < this.length; k++) {
  70.             if (func(this[i]) < func(this[k])) {
  71.                 m = this[k];
  72.                 this[k] = this[i];
  73.                 this[i] = m;
  74.             }
  75.         }
  76.     }
  77.     return this;
  78. }
  79. Array.prototype.OrderByDesc = function (func{
  80.     var m = {};
  81.     for (var i = 0; i < this.length; i++) {
  82.         for (var k = 0; k < this.length; k++) {
  83.             if (func(this[i]) > func(this[k])) {
  84.                 m = this[k];
  85.                 this[k] = this[i];
  86.                 this[i] = m;
  87.             }
  88.         }
  89.     }
  90.     return this;
  91. }


评价

JQERY添加<font color='#ff5555'>拓展方法

给JQERY添加全局方法$.myfunc=function(){ //方法体; } //调用 $.myfunc(); //$.fn不能直接使用,前面需要一...

简化ORM-EF的Lambda查询后排序字段多判断繁琐,并利用表达式树自定义排序<font color='#ff5555'>拓展方法

问题详述: 假设我们实例化一个学生成绩对象,其对象及成员如下:public class Model(){ public int Id { get; set;...

<font color='#ff5555'>js与controller中分割字符串的方法

js: varstr=OpenRule; varstrs=newArray(); strs=str.split(&quot;,&quot;); for(vari=0;i&lt;strs.length;i++){ $(&q...

珍藏!! <font color='#ff5555'>js cSS不传之秘集合

&lt;!--其中5指每隔5秒刷新一次页面--&gt; &lt;metahttp-equiv=&quot;refresh&quot;content=&quot;5&quot;&gt; =========...

<font color='#ff5555'>jsON对象和color='#ff5555'>jsON字符串的区别

JSON对象是直接可以使用JQuery操作的格式,如C#中可以用对象(类名)点出属性(方法)一样var str2 = { &quot;name&quot;: ...

对象转<font color='#ff5555'>jsON

/// &lt;summary&gt; /// 对象转json /// &lt;/summary&gt; public static string ToJson(object jsonObject) ...

来!!!今天玩 <font color='#ff5555'>json

别怪我懒,其实我是真的懒///&lt;summary&gt; ///List转成json ///&lt;/summary&gt; ///&lt;typeparamname=&quot;T&quot...

<font color='#ff5555'>js遍历localStorage的键值对

//遍历本地存储localStorage for(vari=0;i&lt;localStorage.length;i++){ varkey=localStorage.key(i);//获取本地存储的K...

<font color='#ff5555'>js的localStorage

localStorage的优势1、localStorage拓展了cookie的4K限制2、localStorage会可以将第一次请求的数据直接存储到本地,这个相...

<font color='#ff5555'>js监听input、keydown,有输入法时打字完成后触发事件

在给输入框绑定input或keydown事件时预期效果是有输入法时,输入中文后触发事件,不希望输一个字母就触发一次事件可以用到c...

​<font color='#ff5555'>js截取两个字符中间的字符串

使用正则表达式来取比如我们要取brush:js;toolbar:false 这个字符串冒号和分号中间的字符串可以varclassname=&quot;brush:j...

Vue.<font color='#ff5555'>js+Layer实现表格数据绑定与更新

一:使用Vue.js绑定好数据与更新事件 使用v-on绑定好事件,在事件里边直接把该行数据传递进去,在更新方法里边就可以直接...

<font color='#ff5555'>js使用正则进行常用验证

?:问号前面的数字匹配0次或1次,也就是懒惰匹配,最多匹配一次+:匹配1次到无穷次*:匹配0次到无穷次验证只能:数字、字母...

<font color='#ff5555'>js字符串的替换

vartitle=&quot;我是你的大表哥&quot;; title=title.replace(&quot;大表哥&quot;,&quot;大表妹&quot;); alert(title);输...

Vue.<font color='#ff5555'>js 实现省市联动

HTML代码&lt;divid=&quot;pro_citys&quot;&gt; 省:&lt;selectid=&quot;provice&quot;v-on:change=&quot;prochange()&quo...
不需要签名
排名
56
文章
8
粉丝
0
评论
1
Oracle中MERGE INTO,for,start with,decode用法
剑轩 : 多来一点点解释就更好了
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术