Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ajax In Action (2006).pdf
Скачиваний:
63
Добавлен:
17.08.2013
Размер:
8.36 Mб
Скачать

Resources 617

The function getAge() is defined only once, and, because it is attached to the prototype, it is accessible to every Robot that we create.

Closures have their uses, but we’d consider them an advanced topic. If you do want to explore closures in greater depth, then Jim Ley’s article, listed in the Resources section, is a good place to start.

B.4 Conclusions

We’ve taken you through some of the stranger and more interesting features of the JavaScript language in this appendix, with two purposes in mind. The first was to show the expressive power of the language. The second was to point out several traps for the unwary, in which thinking in an OO style may result in suboptimal or even dangerous code.

We’ve looked at JavaScript’s support for objects and at the similarities between the Object and Array classes. We’ve seen several ways of instantiating JavaScript objects, using JSON, constructor functions, and the prototype concept. Along the way, we’ve discussed how to tackle OO concepts such as inheritance and interfaces in JavaScript, in ways that work with rather than against the language.

In our exploration of JavaScript Function objects, we saw how functions can exist independently of any object to which they are assigned and even be borrowed or swapped between objects. We used this knowledge to get a better understanding of the JavaScript event model. Finally, we looked at closures and saw how some common programming idioms can create closures unintentionally, potentially resulting in memory leaks.

Compared to Java or C#, JavaScript offers a great deal of flexibility and scope for developing personal styles and approaches to the language. This can be liberating for the individual programmer, provided that you know what you are doing. It can also present problems when working in teams, but these problems can be alleviated by shared coding conventions or house styles.

JavaScript can be a very enjoyable language to work with, once you understand what makes it tick. If you’re coming to Ajax from a structured OO background, we hope that this chapter has helped you to cross the gap.

B.5 Resources

There are very few books on JavaScript the language, as opposed to web browser programming. David Flanagan’s JavaScript: The Definitive Guide (O’Reilly, 2001) is the definitive work. It’s a bit old, but a new version is due out next year. A more

618APPENDIX B

JavaScript for object-oriented programmers

recent book, Nicholas Zakas’s Professional JavaScript for Web Developers (Wrox, 2004) offers a good language overview, too, and covers some more recent developments in the language.

On the Web, Doug Crockford discusses object-oriented approaches to JavaScript, such as creating private members for classes (www.crockford.com/javascript/private.html) and inheritance (www.crockford.com/javascript/private.html). Peter-Paul Koch’s Quirksmode site (http://quirksmode.org) also discusses many of the finer points of the language. Jim Ley’s discussion of closures in JavaScript can be found at http://jibbering.com/faq/faq_notes/closures.html.

Mike Foster’s x library can be found at www.cross-browser.com.