JavaScript-String类的replace,split 和 创建对象

String类🥱

字符串模式匹配

replace

var text = "cat, bat, sat, fat";
var pattern = /.at/;
//下面与pattern.exec(text)相同
var matches = text.match(pattern);
alert(matches.index);//0字符串第一次出现位置
alert(matches[0]);//"cat" 第一个匹配的字符串
alert(pattern.lastIndex);//0 下次搜索开始位置 非全局/.at/g 位置一直是0;

// String.replace 第一个参数可以传入正则表达式
var result = text.replace("at","ond");
alert(result);
result = text.replace(/at/g,"ond");
alert(result);//这两个结果相同

//replace 第二个字符串如果是参数,可以使用特殊的字符序列
result = text.replace(/(.at)/g,"word ($1)");
//result变为  //word (cat), word (bat)...

//String.replace 可以传入自定义函数
function htmlEscape(text) {
    return text.replace(/[<>"&]/g, function (match, pos, originalText) {
        switch (match) {
            case "<":
                return "&lt;";
            case ">":
                return "&gl;";
            case "&":
                return "&amp;";
            case "\"":
                return "&quot;";
        }
    })
}


alert(htmlEscape("<p class=\"greeting\">Hello world!</p>"));
//&lt;p class=&quot;greeting&quot;&gl;Hello world!&lt;/p&gl;

split

function c(...a){
    console.log(...a);
}
var a = 'a1a2a123';
c(a);
c(a.split(/\d+/),a,!1,!0);

/*
Array (4)
0 "a"
1 "a"
2 "a"
3 ""
“Array”原型
"a1a2a123"
false
true
Array (4)
"a1a2a123"
false
*/
c(a.split(/(\d+)/),a);

/*
Array (7)
0 "a"
1 "1"
2 "a"
3 "2"
4 "a"
5 "123"
6 ""
“Array”原型
"a1a2a123"
*/
c(a.split(/[0-9]/),a);
/*
[Log]  (2) (String_split.js, line 2)
Array (6)
0 "a"
1 "a"
2 "a"
3 ""
4 ""
5 ""

“Array”原型
"a1a2a123"
*/
var colorText = "red,blue,green,yellow";
//正则表达式需要注意的点
var colors3 = colorText.split(/[^\,]+/); //["", ",", ",", ",", ""]
//字符串的方法 和 正则表达式用法对比
c(colors3,/red/.exec(colorText),colorText.search(/r/ig))

正则表达式注意点(^)

^ 匹配输入字符串的开始位置,除非在方括号表达式中使用,当该符号在方括号表达式中使用时,表示不接受该方括号表达式中的字符集合。要匹配 ^ 字符本身,请使用 ^。

创建对象

工厂模式

function createPerson(name, age, job){
    var o = new Object();
    o.name = name;
    o.age = age;
    o.job = job;
    o.sayName = function(){
        alert(this.name);
    };
    return o;
}

构造函数模式

  • 没有显示创建对象
  • 直接将属性和方法赋给了this对象
  • 没有return语句
function Person(name, age, job){
    this.name = name;
    this.age = age;
    this.job = job;
    this.sayName = function(){
    	alert(this.name);
    };
}

Prototype原型

组合使用构造函数模式和原型模式

function Person(name, age, job){
    this.name = name;
    this.age = age;
    this.job = job;
    this.friends = ["Shelby","court"];
}

Person.prototype = {
    constructor : Person;
    sayName : function (){
        alert(this.name)
    }
}

var person1 = new Person("Nicholas",29,"Software Engineer");
var person2 = new Person("Greg",27,"Doctor");

person1.friends.push("Van");
alert(person1.friends);//"Shelby","court","Van"
alert(person2.friends);//"Shelby","court",
alert(person1.friends === person2.friends);//false  ===全等 判断引用 ==等于 判断值
alert(person1.sayName === person2.sayName);//ture

动态原型模式

function Person(name, age, job){
		this.name = name;
  	this.age = age;
  	this.job = job;
  //方法
  	if (typeof this.sayName != "function"){
    	Person.prototype.sayName = function(){
      	alert(this.name);
    	};
  	}
}

只有在初次调用,sayName()方法不存在的时候才会执行方法那段代码

使用动态原型模式时,不能使用对象字面量重写原型,如果在已经创建实例情况下重写原型,会写断现有实例与新原型之间的联系,现有实例原型“指针”仍指向原来的原型

寄生函数构造模式

这个很像典型的构造函数

function Person(name, age, job){
    var o = new Object();
    o.name = name;
    o.age = age;
    o.job = job;
    o.sayName = function (){
        alert(this.name);
    }
    return 0;
}

这个模式可以在特殊情况下用来为对象创建构造函数

function SpecialArray() {
    var values = new Array();
    //apply两个参数,作用域和arguments对象
    values.push.apply(values, arguments);
    values.toPipedString = function () {
        return values.join("|");
    };
    return values;
}

apply和call

call()和apply()方法的作用相同,他们区别在于接受参数的方式不同。

apply方法接收两个参数,一个是运行函数作用域(this),第二个参数可以是数组,也可以是arguments对象。

call方法第一个参数相同,但是其余参数必须直接传递给参数,逐个列举出来。

稳妥构造函数模式

稳妥对象适合在一些安全环境中(禁用this和new),或者防止数据被其他应用程序修改(例如Mashup,不知道是啥,介绍:Mashup是一种令人兴奋的交互式Web应用程序,它利用了从外部数据源检索到的内容来创建全新的创新服务。它们具有第二代Web应用程序的特点,也称为Web2.0。)

function Person(name, age, job) {
    var o = new Object();
    //可以在这里定义私有变量和函数
    o.sayName = function () {
        alert(name);
    }
    return o;
}
var friend = new Person("Nicholas", 29, "Software Engineer");
var friend2 = new Person("Nicholas2", 29, "Software Engineer");
friend2.sayName();
friend.sayName();

说实话这个入参是保存在arguments对象中吗,他们怎么存储这个值不是特别李姐

posted @ 2021-09-16 17:01  _Salvatore  阅读(94)  评论(0)    收藏  举报