Skip to content

Instantly share code, notes, and snippets.

@mattijs
Created August 14, 2012 19:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattijs/3351832 to your computer and use it in GitHub Desktop.
Save mattijs/3351832 to your computer and use it in GitHub Desktop.
Node Object comparison bug
// 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
// 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