การดำเนินการเปรียบเทียบ === ไม่ทำงานบน Chrome ใช่ไหม [ทำซ้ำ]

ฉันไม่รู้ว่าทำไม แต่โค้ดด้านล่างใช้งานได้ใน Firefox แต่ไม่ใช่ใน Google Chrome เพราะเหตุใด นี่ควรเป็น JS มาตรฐาน

status = parseInt($('#input-status').val());
// status field is exactly equals to 0
if (status === 0) {
// do something, in Firefox the code reaches here, in Chrome NOT !
}

นี่คือวิธีกำหนด #input-status ใน html :

<input type="hidden" id="input-status" name="input-status" value="00">

ใน Chrome รหัสจะทำงานเฉพาะในกรณีที่ฉันแทนที่ === โดย ==

มีความคิดอะไรบ้าง?


person delphirules    schedule 12.02.2014    source แหล่งที่มา
comment
ทำ console.log(typeof status); ก่อนคำสั่ง if - Chrome บอกว่าเป็นประเภทอะไร   -  person Anthony Grist    schedule 12.02.2014
comment
คุณแน่ใจหรือไม่ว่า Chrome แยกวิเคราะห์สตริงได้สำเร็จ เพราะนี่น่าจะเป็นปัญหา   -  person ppoliani    schedule 12.02.2014
comment
อย่าลืมระบุฐานรากของคุณ--parseInt($("#input-status").val(), 10);   -  person Elliot Bonneville    schedule 12.02.2014
comment
Chrome บอกว่าสถานะเป็นสตริง แต่ถ้าฉันเรียก parseInt ทำไมมันถึงไม่เป็นจำนวนเต็ม   -  person delphirules    schedule 12.02.2014
comment
@delphirules นั่นคือ จริง รหัสของคุณ ถูกต้อง หรือไม่   -  person Pointy    schedule 12.02.2014
comment
ใช่. นี่คือรหัสของฉันสำหรับฟิลด์อินพุตสถานะ : ‹input type=hidden id=input-status name=input-status value=00›   -  person delphirules    schedule 12.02.2014


คำตอบ (1)


ฉันไม่แน่ใจว่าทำไมสิ่งนี้ถึงเกิดขึ้น แต่ฉันสามารถทำซ้ำได้ดังนี้:

jsfiddle

status = parseInt($('#input-status').val(), 10);
test = parseInt($('#input-status').val(), 10);

console.log(typeof status); // string
console.log(typeof test); // number

ฉันเดาว่า status ถูกกำหนดไว้ล่วงหน้าโดยเบราว์เซอร์และไม่สามารถแยกวิเคราะห์ได้ เพื่อให้ใช้งานได้ให้ใช้ var เพื่อกำหนดตัวแปรใหม่ในขอบเขตปัจจุบัน:

var status = parseInt($('#input-status').val(), 10);

if (status === 0) {
    // this also works in chrome
}

แก้ไข

console.log(window.status === status); // true

ดูเหมือนว่า status เป็นการอ้างอิงถึงวัตถุ window.status ซึ่งเปลี่ยนแถบสถานะในเบราว์เซอร์ และมันก็สมเหตุสมผลแล้วที่แยกวิเคราะห์เป็นตัวเลขไม่ได้

@ Xotic750 ชี้ให้เห็น:

ใช่ บน Chrome เมื่อใดก็ตามที่คุณตั้งค่าตัวแปรร่วม status/window.status เป็นค่าใดๆ window.status = {} จากนั้นจะแปลงเป็นสตริง {value: "[object Object]", writable: true, enumerable: true, configurable: true} บทเรียน jsfiddle: อย่าใช้ตัวแปรร่วม ใช้ var เพื่อให้มีขอบเขตในเครื่อง

person damian    schedule 12.02.2014
comment
เยี่ยมมากการเพิ่ม 'var' จะช่วยแก้ปัญหาได้! :) - person delphirules; 12.02.2014
comment
@delphirules ดีใจที่ฉันช่วยคุณได้ - person damian; 12.02.2014
comment
ใช่ บน Chrome เมื่อใดก็ตามที่คุณตั้งค่าตัวแปรส่วนกลาง status/window.status เป็นค่าใดๆ window.status = {} จากนั้นตัวแปรจะแปลงเป็นสตริง {value: "[object Object]", writable: true, enumerable: true, configurable: true} jsFiddle บทเรียน: อย่าใช้ตัวแปรร่วม ให้ใช้ var เพื่อให้มีการกำหนดขอบเขตในเครื่อง - person Xotic750; 12.02.2014
comment
แค่อยากย้ำว่าบทเรียนที่คุณควรทำคือ: อย่าใช้ตัวแปรส่วนกลางที่กำหนดโดยปริยาย ใช้ var เสมอ และอย่าใช้ globals ทุกครั้งที่เป็นไปได้ - person Colin DeClue; 12.02.2014