tailieunhanh - Practical prototype and scipt.aculo.us part 33

Practical prototype and part 33: The information in this book is distributed on an "as is" basis, without warranty Although every pre-caution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. | 184 CHAPTER 8 OTHER HELPFUL THINGS USEFUL METHODS ON BUILT-INS var player new Player Johnny Generic player - firstName Johnny lastName Generic points 0 var qb new Quarterback Andrew Dupont qb - position QB firstName Andrew - lastName Dupont points 0 You need only define a toJSON method to tell your object how to encode itself into JSON. The preceding example illustrates one of JSON s drawbacks it isn t a lossless format. Since these classes contain functions there s no way they can be converted to JSON and come back wholly intact. But this is the minimum amount of information we need to restore the class as it existed originally. JSON can t do this automatically but we can do it manually without much effort. Using Object Methods Prototype defines quite a few methods on Object the generic constructor and patriarch of the JavaScript family. Unlike the String methods just covered these aren t instance methods they re attached to Object itself. Type Sniffing with The hasty conception and standardization of JavaScript in the 1990s left a few gaping holes in the language. One of the biggest holes is the typeof operator in theory it gives us useful information about an item but in practice it acts like Captain Obvious. Sure it handles the simple cases just fine typeof syzygy - string typeof 37 - number typeof false - boolean CHAPTER 8 OTHER HELPFUL THINGS USEFUL METHODS ON BUILT-INS 185 But when it s asked to do more complex checks it falls flat on its face typeof 1 2 98 - object typeof new Date - object typeof new Error OMG - object Arrays Dates and Errors are objects of course but so are all non-primitives. Imagine if you asked me Do you know what time it is and I responded only with Yes. My answer is both narrowly correct and completely useless. The one other value returned by typeof is function but that applies both to functions and regular expressions typeof - function typeof ba r z - function In other words typeof arbitrarily .

TỪ KHÓA LIÊN QUAN