js代码练习仓库
String.prototype用于为某字符串对象新增方法,比如:
String.prototype.replaceAll = function(oldStr, newStr) {
return this.replace(new RegExp(oldStr,"gm"),newStr);
}
var a="dsdsdsadadasdad";
a.replaceAll("a","c");
"dsdsdscdcdcsdcd"
var str1 = new String('haha');
String.prototype.trim.call(str1);
function Person(name, age) {
this.name = name;
this.age = age;
this.alertName = function () {
alert(this.name);
}
this.alertAge = function () {
alert(this.age);
}
}
var a=new Person();
Person("dssd",90);
alertAge();//90
alertName();//dssd
function webDever(name, age, sex) {
//
Person.call(this, name, age);
this.sex = sex;
this.alertSex = function () {
alert(this.sex);
}
}
String.prototype.say=function(){
alert(this);
};
"test".say();