TAILIEUCHUNG - Practical prototype and scipt.aculo.us part 52

Practical prototype and part 52: 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 13 PROTOTYPE AS A PLATFORM 303 Brilliant Now to grab a class instance from the associated element we can use its ID var somelnstance We could stop right here and be happy with ourselves. But let s consider some edge cases first What if the element doesn t have an ID Then our key will be null. To ensure that the element has an ID we can use Element identify. The method returns the element s ID if it already exists if not it assigns the element an arbitrary ID and returns it to us. What if we don t know the name of the class In order to move this code into a mixin we ll have to remove any explicit references to the class s name. Luckily we ve already got the answer to this one the instance s constructor property which points back to the class itself. What if that element already has an instance and another one gets created For this case we ll assume that only one instance per element is needed. When a new one gets declared it would be nice if we cleaned up the old one somehow. First let s make a mixin called Trackable. It ll contain the code for keeping track of a class s instances. Let s also create a register method which should be the only one we need for this exercise. It ll add the instance to the lookup table. var Trackable register function Now we ll solve our problems one by one. First let s grab the element s ID. If the class doesn t have an element property we ll simply return false. You may choose to throw an exception instead just make sure you handle this case one way or another. var Trackable register function if return false var id Next we ll use the constructor property to reach the class itself. This way we don t have to call it by name. We ll also create the instances property if it doesn t already exist. 304 CHAPTER 13 PROTOTYPE AS A PLATFORM var Trackable register function if return false var id var c if .

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã 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.