在JS里面自定义需要的属性

  • 原创
  • |
  • 浏览:173
  • |
  • 更新:

在项目中有时候需要通过js判断是否为空、数组中是否包含某对象、字符串去空等操作。

方法/步骤

  1. 1

    //自定义数组包含方法

    Array.prototype.contains = function ( obj) {

      for (i in this) {

        if (this[i] == obj) return true;

      }

      return false;

    }

  2. 2

    //自定义字符串不为空

    String.prototype.isNotBlank = function(){

    return (null != this && '' != this);

    }

  3. 2
    此文章未经许可获取自百度经验
  4. 3

    /去左右空格;

    String.prototype.trim=function()

     return this.replace(/(^s*)|(s*$)/g, "")

    }

    //去除左边的空格

    String.prototype.ltrim=function()

     return this.replace(/(^s*)/g,"")

     }

    //去除右边的空格

    String.prototype.rtrim=function(){return this.replace(/(s*$)/g,"")

    }

    END
经验内容仅供参考,如果您需解决具体问题(尤其法律、医学等领域),建议您详细咨询相关领域专业人士。
作者声明:本篇经验系本人依照真实经历原创,未经许可,谢绝转载。
展开阅读全部