prototype 扩展或者覆盖类中的方法。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=<device-width>, initial-scale=1.0">
<title>Document</title>
</head>
<script>
//低版本的ie浏览器不支持字符串的trim()函数,可以对String类扩展一个全新的trim()函数
String.prototype.trim = function(){
return this.replace(/^\s+/,"").replace(/\s+$/,"");
}
window.onload = function(){
document.getElementById("btn").onclick=function(){
var username = document.getElementById("username").value;
username = username.trim();
alert("---->"+username+"<----")
}
}
</script>
<input type="text" id="username"/>
<input type="button" value="获取用户名" id="btn"/>
</body>
</html>