TAILIEUCHUNG - Phát triển Javascript - part 17

Tham khảo tài liệu 'phát triển javascript - part 17', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Creating Objects with Constructors 133 function return Listing shows a simple test to verify that objects do indeed inherit the methods. Listing Testing test should inherit properties from function var circle new Circle 6 assertEquals 12 Repeating quickly becomes cumbersome and expensive in terms of bytes to go over the wire when adding more than a few properties to the prototype. We can improve this pattern in a number of ways. Listing shows the shortest way simply provide an object literal as the new prototype. Listing Assigning diameter function return 2 circumference function return area function return Unfortunately this breaks some of our previous tests. In particular the assertion in Listing no longer holds. Listing Failing assertion on constructor equality assertEquals Circle Download from 134 Objects and Prototypal Inheritance When we assign a new object to JavaScript no longer creates a constructor property for us. This means that the Get for constructor will go up the prototype chain until a value is found. In the case of our constructor the result is whose constructor property is Object as seen in Listing . Listing Broken constructor property test constructor is Object when prototype is overridden function function Circle assertEquals Object new Circle .constructor Listing solves the problem by assigning the constructor property manually. Listing Fixing the missing constructor property constructor Circle . To avoid the problem entirely we could also extend the given prototype property in a closure to avoid repeating for each property. This approach is shown

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.