Created
August 14, 2012 19:12
-
-
Save mattijs/3351832 to your computer and use it in GitHub Desktop.
Node Object comparison bug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Values we check against | |
var obj = {}; | |
var str = 'foo'; | |
// Two identical functions | |
var isObjectA = function(o) { | |
return o === Object(o); | |
}; | |
var isObjectB = function(o) { | |
return o === Object(o); | |
}; | |
// | |
// # Triggers | |
// | |
// 1: Useless comparison statement, this is half the trigger | |
obj == obj; | |
// ... or require punycode module as trigger instead | |
//require('punycode'); | |
// 2: Initial check with object, this is the other half of the trigger | |
isObjectA(obj); | |
// | |
// # Checks | |
// | |
// String passes as Object in isObjectA. | |
console.log('isObjectA(str):', isObjectA(str)); | |
// But does not pass in isObjectB ... | |
console.log('isObjectB(str):', isObjectB(str)); | |
// or in an inline comparison | |
console.log('inline check: ', (str === Object(str))); | |
// Remove any one of the triggers and isObjectA(str) behaves normal |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Values we check against | |
var obj = {}; | |
var str = 'foo'; | |
// Two identical functions | |
var isObjectA = function(o) { | |
return o === Object(o); | |
}; | |
var isObjectB = function(o) { | |
return o === Object(o); | |
}; | |
// | |
// # Triggers | |
// | |
// 1: Useless comparison statement, this is half the trigger | |
obj == obj; | |
// ... or require punycode module as trigger instead | |
//require('punycode'); | |
// 2: Initial check with object, this is the other half of the trigger | |
isObjectA(obj); | |
// | |
// # Checks | |
// | |
// String passes as Object in isObjectA. | |
print('isObjectA(str):', isObjectA(str)); | |
// But does not pass in isObjectB ... | |
print('isObjectB(str):', isObjectB(str)); | |
// or in an inline comparison | |
print('inline check: ', (str === Object(str))); | |
// Remove any one of the triggers and isObjectA(str) behaves normal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment