简体   繁体   English

JavaScript 严格类型检查失败

[英]JavaScript strict type checking fails

I was checking three Strings equality;我正在检查三个字符串是否相等;

var str1 = "Hello World!";
var str2 = "Hello \
    World!";
var str3 = "Hello" + " World!";
//console.log(str1===str2);//true
// console.log(str2===str3);//true
console.log(str1 === str2 === str3); //false

How can we check this scenario where I want to compare 3 variables?我们如何检查我想比较 3 个变量的情况?

Your question isn't totally clear to me.你的问题对我来说并不完全清楚。

Assuming your question is about how to compare the equality of 3 strings:假设您的问题是关于如何比较 3 个字符串的相等性:

With

console.log(str1 === str2 === str3);

you're checking你在检查

(str1 === str2) === str3

(yes, the associativity is left to right here) (是的,结合性在这里从左到右

That is, you're comparing a boolean to a string.也就是说,您将布尔值与字符串进行比较。

What you want is你想要的是

(str1 === str2) && (str2 === str3)

Assuming your question is about how to compare strings but not caring about the numbers and types of spaces (see note below):假设您的问题是关于如何比较字符串但不关心空格的数量和类型(请参见下面的注释):

Then the best would be to normalize your strings using a function like那么最好的方法是使用类似的函数来规范化你的字符串

function normalize(str){
    return str.replace(/\s+/g,' ');
}

And you can define a function comparing all your strings:您可以定义一个比较所有字符串的函数:

function allEquals(strs) {
   var p;
   for (var i=0; i<arguments.length; i++) {
       if (typeof arguments[i] !== "string") return false;
       var s = arguments[i].replace(/\s+/g, ' ');
       if (p !== undefined && s!==p) return false;
       p = s;
   }
   return true;
}

console.log(allEquals(str1, str2, str3));

Note: multi-line string literals are painful.注意:多行字符串文字很痛苦。 Those strings are different:这些字符串是不同的:

var str2 = "Hello \
    World!";
var str4 = "Hello \
World!";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 组件的@Input严格类型检查 - Strict type checking of @Input for component javascript:MIME类型(&#39;text / html&#39;)无法执行,并且启用了严格的MIME类型检查 - javascript: MIME type ('text/html') is not executable, and strict MIME type checking is enabled 加载模块脚本失败:服务器以非 JavaScript MIME 类型“”响应。 强制执行严格的 MIME 类型检查 - Failed to load module script: The server responded with a non-JavaScript MIME type of “”. Strict MIME type checking is enforced 禁用 Chrome 严格的 MIME 类型检查 - Disable Chrome strict MIME type checking 在调用Java脚本之前检查其严格的模式兼容性 - Checking for strict mode compatibility in Javascript before invoking it 检查Javascript .split()是否失败 - Checking if Javascript .split() fails 服务器以“application/octet-stream”的非 JavaScript MIME 类型响应 每个 HTML 对模块脚本强制执行严格的 MIME 类型检查 - The server responded with a non-JavaScript MIME type of "application/octet-stream" Strict MIME type checking is enforced for module scripts per HTML 加载模块脚本失败:服务器以“text/plain”的非 JavaScript MIME 类型响应。 对模块强制执行严格的 MIME 类型检查 - Failed to load module script: The server responded with a non-JavaScript MIME type of "text/plain". Strict MIME type checking is enforced for module 加载模块脚本失败:服务器以“text/plain”的非 JavaScript MIME 类型响应。 严格的 MIME 类型检查 i - Failed to load module script: The server responded with a non-JavaScript MIME type of "text/plain". Strict MIME type checking i MIME 类型 ('application/json') 不可执行,并且启用了严格的 MIME 类型检查 - MIME type ('application/json') is not executable, and strict MIME type checking is enabled
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM