This is the 24th day of my participation in Gwen Challenge

preface

There are many new properties, methods, and features in ES6, and this series will re-learn ES6 through simulation implementations to help you better understand those new things.

Objective: To understand the ES6 string addition method through simulation

Note: in the article only do a simple introduction to each method, and then simulation implementation

Analog implementation

New string lookup method

includes()

introduce

Argument: Accepts a substring that represents what you are looking for

Used to find if a string contains what you are looking for

Return value: returns true or false, true meaning found, false meaning not found

Simulation implementation:_includesmethods

String.prototype._includes = function (findStr) {
    return this.indexOf(findStr) == -1 ? false : true;
}
let test = "abcd"._includes("abc");
console.log(test)//true
Copy the code

startsWith()

introduce

Parameter: Accepts a substring representing what is being tested

Purpose: Checks whether a string already begins with someone

Return value: Returns true or false

Simulation implementation:_startsWithmethods

String.prototype._startsWith=function(str){
    return this.slice(0,str.length) == str
}
console.log("abcd"._startsWith("ab"));
Copy the code

endsWith()

introduce

Parameter: Receives a substring representing what to detect

Function: Used to detect whether there has been any end

Return value: Returns true or false

Simulation implementation:_endsWithmethods

String.prototype._endsWith = function(str){
    return this.slice(-str.length) == str
}
console.log("abcd"._endsWith("ab"));//false
console.log("abcd"._endsWith("cd"));//true
Copy the code

String repetition

repeat(n)

introduce

Parameter: Accepts a number n as an argument, indicating the number of repetitions

Function: Repeat a string n times

Return value: Returns a new string containing n initial strings

Simulation implementation:_repeatmethods

String.prototype._repeat = function (n) {
    let self =  this.valueOf();
    let res = "";
    let temp = Number(n) || 0;
    if (temp) {
        for (let i = 0; i < temp; i++) {
            res +=self;
        }
        returnres; }}console.log("abc"._repeat(2));
Copy the code

Fill string

padStart()

introduce

Arguments: Accepts two arguments, the first representing the length of the entire filled string and the second representing what was filled

Function: Inserts a string before a string

Return value: Returns the populated string

Simulation implementation:_padStartmethods

String.prototype._padStart = function (len, padString) {
    let str = this;
    padString = padString.toString();
    if (padString.length > len - this.length) {
        padString = padString.toString().slice(0, len - this.length);
        while(str.length < len) { str += padString; }}// padstring. length is less than len-this. length.
    // If padString has no content, each fill position is filled with a space;
    // If there is content, repeat it (sort of like picture repeat fill)
    else if(padString.length == 0) {while (str.length < len) {
            str = ""+ str; }}else{
        // Characters in padString may need to be repeated
        // I need to fill in the len-this.length bit, and the string I fill in is padString.length
        while(str.length < len) { str = padString + str; }}return str;
}
console.log("abc"._padStart(6."122111"));
Copy the code

padEnd()

introduce

Parameters: As with padStart, it takes two parameters, the first parameter indicating the length of the entire string and the second parameter indicating what to fill in

Function: Inserts a string after a string

Return value: Returns the populated string

Simulation implementation:_padEndmethods

String.prototype._padEnd = function (len, padString) {
    let str = this;
    padString = padString.toString();
    if (padString.length > len - this.length) {
        padString = padString.toString().slice(0, len - this.length);
        while(str.length < len) { str = str + padString; }}else if (padString.length == 0) {
        while (str.length < len) {
            str += ""; }}else {
        while(str.length < len) { str += padString; }}return str;
}
console.log("abc"._padEnd(6."122111"));
Copy the code

Trim (), trimStart(), trimEnd()

introduce

Parameters: no

Function: trim used to remove the Spaces, trimStart used to remove the left space on both sides of the string, trimEnd used to remove the right Spaces! For good memory, browsers implement trimLeft() and trimRight() for trimStart() and trimEnd(), respectively

Return value: All returns the new string without whitespace, without changing the original string

Simulation implementation:_trim(),_trimStart(),_trimEnd()

String.prototype._trim = function () {
    let reg = /(^\s*)|(\s*$)/g
    return this.replace(reg, ' ')}String.prototype._trimStart = function () {
    let reg = /(^\s*)/g
    return this.replace(reg, ' ')}String.prototype._trimEnd = function () {
    let reg = /(\s*$)/g
    return this.replace(reg, ' ')}let arr = " abc "
console.log(arr._trim(),arr._trimStart(),arr._trimEnd());
Copy the code

END

Above is a mock implementation of some new methods on ES6 strings!

The real implementation may be different from what I wrote, but it doesn’t matter, as long as we are familiar with what these methods do, we have achieved the purpose of this study!

If you have any questions or suggestions, please leave a message. Thank you!