SOURCE

// 实现一个字符串匹配算法 indexOf
String.prototype.myIndexOf = function (str, startPosition = 0){
    if(typeof str!=="string"){
        throw TypeError("The first argument must be string")
    }
    let fatherStr = this
    let len = str.length
    for(let i = startPosition; i<=fatherStr.length-len; i++){
        if(fatherStr.substring(i, i+len)===str){
            return i
        }
    }
    return -1
}

let father = "abcdefgcde"
let son = "cde"
console.log(father.myIndexOf("",3))
console 命令行工具 X clear

                    
>
console