我目前在 JavaScript 中使用严格的 === 比较,该比较会根据我的计算机上的要求生成预期结果,但部署到服务器时却不会,其行为会有所不同。
脚本
computed: {
type: function(){
switch(this.job.type)
{
case 1:
return 'Request Quote';
case 2:
return 'Negotiate';
case 3:
return 'Fixed';
default:
return 'matched none';
}
},
}
模板
<p class="text-primary"><strong>{{type}}</strong></p>
本地服务器上作业类型 2 的输出
如果我将代码切换到宽松的比较 If 语句,它会很好地工作
如果语句
computed: {
type: function(){
if(this.job.type == 1) return 'Request Quote';
else if(this.job.type == 2) return 'Negotiate';
else if(this.job.type == 3) return 'Fixed';
else return 'matched none';
},
}
正如您可能已经注意到的,我正在使用 VUEJS 框架,作业对象也是使用 axios 获取的数据库模型。我还在后端使用 Laravel。
会不会是mysql版本的问题?
最佳答案
这意味着您的代码/配置/等。在生产中创建一个字符串,或一个数字对象(而不是基元),或某种其他类型的对象,当强制转换为数字时,该对象会强制转换为您的预期值之一。
带有字符串的示例:
function strict(type) {
switch(type)
{
case 1:
return 'Request Quote';
case 2:
return 'Negotiate';
case 3:
return 'Fixed';
default:
return 'matched none';
}
}
function loose(type) {
if(type == 1) return 'Request Quote';
else if(type == 2) return 'Negotiate';
else if(type == 3) return 'Fixed';
else return 'matched none';
}
var n;
console.log("Number:");
n = 2;
console.log("strict", strict(n));
console.log("loose", loose(n));
console.log("String:");
n = "2";
console.log("strict", strict(n));
console.log("loose", loose(n));
数字对象示例:
function strict(type) {
switch(type)
{
case 1:
return 'Request Quote';
case 2:
return 'Negotiate';
case 3:
return 'Fixed';
default:
return 'matched none';
}
}
function loose(type) {
if(type == 1) return 'Request Quote';
else if(type == 2) return 'Negotiate';
else if(type == 3) return 'Fixed';
else return 'matched none';
}
var n;
console.log("Primitive:");
n = 2;
console.log("strict", strict(n));
console.log("loose", loose(n));
console.log("Number object:");
n = new Number(2);
console.log("strict", strict(n));
console.log("loose", loose(n));
您必须找出环境/代码/配置中的不同之处,以便您最终得到错误类型的 this.job.type
值。
关于javascript - 严格比较在生产环境中未产生预期结果,但在本地服务器上运行良好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48685099/