Practical prototype and scipt.aculo.us part 4

Practical prototype and part 4: 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 1 WHAT YOU SHOULD KNOW ABOUT PROTOTYPE JAVASCRIPT AND THE DOM 5 These are the constructors for data types. But instances of these data types also inherit from Object foo bar instanceof Object Array literal - true . instanceof Object RegExp literal - true new Date instanceof Object - true But here s where it gets tricky. The typical primitives in a programming language strings numbers and Booleans are both primitives and objects in JavaScript. They re treated as one or the other depending on context. foo instanceof Object - false new String instanceof Object - true 5 instanceof Object - false new Number 5 instanceof Object - true true instanceof Object - false new Boolean true instanceof Object - true This is confusing at first but ends up being quite helpful. It allows these types to behave like primitives when they need to they re passed by value instead of by reference but they can still reap the benefits of JavaScript s object-oriented functionality instance methods local scope etc. . All Objects Have Prototypes Although JavaScript is most certainly object oriented it s likely not the sort of object orientation you re used to. Strictly speaking there is no concept of a class in JavaScript instead of outlining an abstract definition of an object you can simply make a copy of an existing object. Remember what we just found out Array instanceof Object - true new Array instanceof Object - true There is no technical distinction between Array and instances of Array. You can do the same sorts of things to either one. Because there are no classes in JavaScript inheritance is based on the concept of prototypes. Each object has its own prototype property which serves as a template for any new instances copies made from that object. 6 CHAPTER 1 WHAT YOU SHOULD KNOW ABOUT PROTOTYPE JAVASCRIPT AND THE DOM This behavior isn t limited to user-defined objects it can be applied to the built-ins as well. It s quite simple to add instance methods to arrays strings or any .

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.