Practical prototype and scipt.aculo.us part 8

Practical prototype and part 8: 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. | CHAPTER 2 PROTOTYPE BASICS 29 Summary You can do far more with Prototype than what I ve just described but the functions in this chapter are the ones you ll use most often. And although they solve common problems they also form the foundation for a general scripting philosophy one that espouses fewer lines of code separation of content and behavior and the principle of least surprise. Later on you ll learn how to use these functions within a set of conventions to make your DOM scripting experience far more pleasant. CHAPTER 3 Collections Or Never Write a for Loop Again Collections are at the heart of DOM scripting arrays hashes DOM NodeLists and various other groups of items. Nearly all your scripts will do some form of iteration over an array. So why is iteration so bland in JavaScript Prototype sports a robust library for dealing with collections. It makes arrays astoundingly flexible and invents Hash a subclass of Object for key value pairs but can also be integrated into any collections you use in your own scripts. The Traditional for Loop Amazingly the first version of JavaScript didn t even support arrays. They were added soon after but with only one real enhancement over a vanilla Object a magic length property that would count the number of numeric keys in the array. For example var threeStooges new Array threeStooges 0 Larry threeStooges 1 Curly threeStooges 2 Moe - 3 The length property and the ubiquitous for looping construct result in a simple low-tech way to loop over an array s values start at 0 and count up to the value of length. for var i 0 i i threeStooges i Nyuk 31 32 CHAPTER 3 COLLECTIONS OR NEVER WRITE A FOR LOOP AGAIN This is a fine and decent way to loop but JavaScript is capable of so much more A language with JavaScript s expressive power can embrace functional programming concepts to make iteration smarter. Functional Programming JavaScript is a multi-paradigm language. It can

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.