Practical prototype and scipt.aculo.us part 27

Practical prototype and part 27: 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 7 ADVANCED JAVASCRIPT FUNCTIONAL PROGRAMMING AND CLASS-BASED OOP 147 23 Andrew Dupont throws for 23yds. - 23 39 Andrew Dupont throws for 39yds. TOUCHDOWN - 62 - 6 Everything works as expected. So let s try another position. A wide receiver plays on offense and catches passes thrown by the quarterback. var WideReceiver Player initialize function firstName lastName call Player s initialize method super firstName lastName define properties for receivers 0 catchPass function yards this catches a pass for yards yds yards catchTouchdown function yards yards TOUCHDOWN 6 Notice again that we re not writing copy-and-paste code. Our WideReceiver class defines only those methods and properties that are unique to wide receivers deferring to the Player class for everything else. 148 CHAPTER 7 ADVANCED JAVASCRIPT FUNCTIONAL PROGRAMMING AND CLASS-BASED OOP Monkeypatching Most OOP-like languages treat classes as static once they re defined they re immutable. In JavaScript however nothing is immutable and it would be silly to pretend otherwise. Instead Prototype borrows from Ruby once again. In Ruby all classes are mutable and can be changed at any point. This practice is referred to as monkeypatching by those who deride it I ll refer to it that way simply because I like words that contain monkey. But there s no negative connotation for me. Each class object comes with a method addMethods that lets us add instance methods to the class later on makeActive function true this will be a starter for Sunday s game. makeReserve function false this will spend Sunday on the bench. So now we ve got two new methods on the Player class for managing team lineups. But these methods also propagate to Player s two .

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.