JS框架都替你做好了,无特殊需要自然不必再用
原生JS中的作用就大了去了,比如try {
  String.trim('');
}catch(e) {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
  }
}
s = '  abc   ';
alert('[' + s.trim() + ']');

解决方案 »

  1.   

    String是个自定义的类么? 通过prototype覆盖了原来的方法?
      

  2.   

    通过原生String类型中的prototype为对象增加trim方法可惜判断的做法是错误的。
    String.trim是直接加在String这个Function的基础上。
    而prototype是针对String对象而言的。if (!String.prototype.trim) {
        String.prototype.trim = function() {
            return this.replace(/^\s+|\s+$/g, '');
        }
    }
    var s = '  abc   ';
    alert('[' + s.trim() + ']');这样才是对的