TypeError 模块中的语句不允许函数在没有第二个参数的情况下工作
Statement in TypeError module is not letting function work without second parameter
我想向 String 对象添加一个函数,该函数搜索我们要查找的单词的所有字符串和 returns 索引。当我不使用 startIndex
参数时,它不应该抛出第二个错误,因为这个语句 typeof startIndex !== "undefined"
让这个函数在没有 startIndex
的情况下工作。请纠正我并感谢您的帮助。
String.prototype.allIndexOf = allIndexOfFunction;
function allIndexOfFunction(string, startIndex) {
startIndex = startIndex || 0
var indexArr = [];
var sIndex = 0;
var baseString = this.concat();
if (typeof string === "string" && typeof startIndex === "number" && startIndex >= 0) {
while(sIndex !== -1){
sIndex = baseString.indexOf(string, startIndex);
if(sIndex !== -1){
indexArr.push(sIndex);
startIndex = startIndex + sIndex +1;
}
}
}
try {
if (typeof string !== "string") {
throw "First parameter must be a string type";
}
else if (typeof startIndex !== "number" || typeof startIndex !== "undefined") {
throw "Second parameter must be a number type";
}
else if (startIndex <= 0) {
throw "Second parameter must be equal or bigger than 0";
}
} catch(err) {
console.log(err);
}
return indexArr;
}
//TEST
var a = "Lorem ipsum dolor sit Buzz, consectetur Buzz elit. Quod vero voluptatibus Buzz error deserunt libero, Buzz incidunt Buzz facere! A!";
var test = a.allIndexOf("Buzz");
console.log("Searching indexes of \"Buzz\" word in string -> " + a);
console.log(test);
简单的问题。您的逻辑不太正确 - 您需要 AND 而不是 OR:
将您的一行更改为:
else if (typeof startIndex !== "number" && typeof startIndex !== "undefined") {
此外,如果未定义 startIndex,则默认为 0,因此根本不需要第二个条件测试。
您可以在此处看到 运行 如预期的那样:
String.prototype.allIndexOf = allIndexOfFunction;
function allIndexOfFunction(string, startIndex) {
startIndex = startIndex || 0
var indexArr = [];
var sIndex = 0;
var baseString = this.concat();
if (typeof string === "string" && typeof startIndex === "number" && startIndex >= 0) {
while(sIndex !== -1){
sIndex = baseString.indexOf(string, startIndex);
if(sIndex !== -1){
indexArr.push(sIndex);
startIndex = startIndex + sIndex +1;
}
}
}
try {
if (typeof string !== "string") {
throw "First parameter must be a string type";
}
else if (typeof startIndex !== "number") {
throw "Second parameter must be a number type";
}
else if (startIndex <= 0) {
throw "Second parameter must be equal or bigger than 0";
}
} catch(err) {
console.log(err);
}
return indexArr;
}
//TEST
var a = "Lorem ipsum dolor sit Buzz, consectetur Buzz elit. Quod vero voluptatibus Buzz error deserunt libero, Buzz incidunt Buzz facere! A!";
var test = a.allIndexOf("Buzz");
console.log("Searching indexes of \"Buzz\" word in string -> " + a);
console.log(test);
我想向 String 对象添加一个函数,该函数搜索我们要查找的单词的所有字符串和 returns 索引。当我不使用 startIndex
参数时,它不应该抛出第二个错误,因为这个语句 typeof startIndex !== "undefined"
让这个函数在没有 startIndex
的情况下工作。请纠正我并感谢您的帮助。
String.prototype.allIndexOf = allIndexOfFunction;
function allIndexOfFunction(string, startIndex) {
startIndex = startIndex || 0
var indexArr = [];
var sIndex = 0;
var baseString = this.concat();
if (typeof string === "string" && typeof startIndex === "number" && startIndex >= 0) {
while(sIndex !== -1){
sIndex = baseString.indexOf(string, startIndex);
if(sIndex !== -1){
indexArr.push(sIndex);
startIndex = startIndex + sIndex +1;
}
}
}
try {
if (typeof string !== "string") {
throw "First parameter must be a string type";
}
else if (typeof startIndex !== "number" || typeof startIndex !== "undefined") {
throw "Second parameter must be a number type";
}
else if (startIndex <= 0) {
throw "Second parameter must be equal or bigger than 0";
}
} catch(err) {
console.log(err);
}
return indexArr;
}
//TEST
var a = "Lorem ipsum dolor sit Buzz, consectetur Buzz elit. Quod vero voluptatibus Buzz error deserunt libero, Buzz incidunt Buzz facere! A!";
var test = a.allIndexOf("Buzz");
console.log("Searching indexes of \"Buzz\" word in string -> " + a);
console.log(test);
简单的问题。您的逻辑不太正确 - 您需要 AND 而不是 OR:
将您的一行更改为:
else if (typeof startIndex !== "number" && typeof startIndex !== "undefined") {
此外,如果未定义 startIndex,则默认为 0,因此根本不需要第二个条件测试。
您可以在此处看到 运行 如预期的那样:
String.prototype.allIndexOf = allIndexOfFunction;
function allIndexOfFunction(string, startIndex) {
startIndex = startIndex || 0
var indexArr = [];
var sIndex = 0;
var baseString = this.concat();
if (typeof string === "string" && typeof startIndex === "number" && startIndex >= 0) {
while(sIndex !== -1){
sIndex = baseString.indexOf(string, startIndex);
if(sIndex !== -1){
indexArr.push(sIndex);
startIndex = startIndex + sIndex +1;
}
}
}
try {
if (typeof string !== "string") {
throw "First parameter must be a string type";
}
else if (typeof startIndex !== "number") {
throw "Second parameter must be a number type";
}
else if (startIndex <= 0) {
throw "Second parameter must be equal or bigger than 0";
}
} catch(err) {
console.log(err);
}
return indexArr;
}
//TEST
var a = "Lorem ipsum dolor sit Buzz, consectetur Buzz elit. Quod vero voluptatibus Buzz error deserunt libero, Buzz incidunt Buzz facere! A!";
var test = a.allIndexOf("Buzz");
console.log("Searching indexes of \"Buzz\" word in string -> " + a);
console.log(test);