- 2008-12-23 (Tue) 11:01
- JavaScript
I’ve read “JavaScript: The Good Parts” a few times already, since it’s a thin but dense book. This book is somehow basic, but full of discerning. I should have do an Advent Calendar-ish thing if I could start this earlier.. But anyway, I will keep writing a bit about what I learned from this book for a while.
So the first thing, is a string comparison, on Chap. 2, page 10.
Following expressions both return true!
('cat' == 'c' + 'a' + 't') // true
('cat' === 'c' + 'a' + 't') // true
Before I read this, I originally thought JavaScript might compare the instance-base equality. I mean if the string instances on both side on the equal sign were pointing to the exactly same memory address, it would return true, but it is actually comparing its value.
var s1 = s2 = 'foo'; var s3 = 'foo'; s1 == s2; // true s1 == s3; // still true, of course.
So, if you wanna do instance base equality, do as follows:
var s4 = new String('foo');
var s5 = new String('foo');
s4 == s5 //false
But as it’s mentioned in the book (and I strongly agreed!), wrapper objects (String, Number and Boolean) are just seeds of confusion. I don’t also see the neccessity of using them.
By the way, I thought that instance-base comaprison is more popular rather than value-base comparison. But as far as I just tested, Java (1.5.0_16 on Intel Mac) compares value, PHP compares value.. that’s intereting.
Comments:0
Trackbacks:0
- Trackback URL for this entry
- http://blog.nydd.org/2008/12/string-comparison-javascript-the-good-parts/trackback/
- Listed below are links to weblogs that reference
- String comparison – “JavaScript: The Good Parts” from Vantage Point of Queens