Created
February 28, 2013 12:02
-
-
Save alucky0707/5056289 to your computer and use it in GitHub Desktop.
obj+""≠obj.toString()≠String(obj)っていう話 ref: http://qiita.com/items/8d80ca107555489710ea
This file contains 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
// obj.toString() | |
str = obj+""; |
This file contains 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
"toString" | |
"valueOf" | |
"toString" |
This file contains 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
"obj1 valueOf" | |
"obj2 toString" | |
TypeError : .... |
This file contains 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
Object.prototype.valueOf = function(){ | |
return this; | |
}; |
This file contains 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
var | |
obj1 = { | |
toString : function(){ | |
return {}; | |
}, | |
valueOf : function(){ | |
rettrn "valueOf"; | |
}, | |
}, | |
obj2 = { | |
toString : function(){ | |
return {}; | |
}, | |
valueOf : function(){ | |
return {}; | |
}, | |
}; | |
console.log(String(obj1)); //=> "valueOf" | |
console.log(String(obj2)); //TypeError! |
This file contains 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
str = String(obj.toString()); |
This file contains 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
var obj = { | |
toString : function(){ | |
return "toString"; | |
}, | |
valueOf : function(){ | |
return "valueOf"; | |
}, | |
}; | |
console.log(obj.toString()); //=> "toString" | |
console.log(obj+""); //=> ??? | |
console.log(String(obj)); // ??? |
This file contains 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
var | |
obj1 = { | |
toString : function(){ | |
return "obj1 toString"; | |
}, | |
valueOf : function(){ | |
return "obj1 valueOf"; | |
}, | |
}, | |
obj2 = { | |
toString : function(){ | |
return "obj2 toString"; | |
}, | |
valueOf : function(){ | |
return {}; | |
}, | |
}, | |
obj3 = { | |
toString : function(){ | |
return {}; | |
}, | |
valueOf : function(){ | |
return {}; | |
}, | |
}; | |
[obj1,obj2,obj3].forEach(function(obj){ | |
console.log(obj+""); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment