---
title: "JS去空格"
author: "iammonster"
date: "2008-12-03T12:38:52Z"
tags: []
slug: "js"
draft: false
lastmod: "2008-12-03T12:38:52Z"
id: 285619
url: "https://jiajun.iteye.com/blog/285619"
---

首先说明：

返回对象类型原型的引用：objectName.prototype

objectName 参数是对象的名称。  
用 prototype 属性提供对象的类的一组基本功能。对象的新实例“继承”赋予该对象原型的操作。

 

举例如下：

 

```
//为String对象添加Trim,LTrim,RTrim方法
//去左右空格
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, "");
}
```