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

Tham khảo tài liệu 'phát triển javascript - part 18', 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ả | Pseudo-classical Inheritance 143 Listing Implementing Sphere with inherit2 function Sphere radius this radius Circle area function return 4 Performance of the super Method Using the inherit2 method we can create constructors and objects that come pretty close to emulating classical inheritance. It does not however perform particularly well. By redefining all the methods and wrapping them in closures inherit2 will not only be slower than inherit when extending constructors but calling will be slower than calling this. this as well. Further hits to performance are gained by the try-catch which is used to ensure that is restored after the method has executed. As if that wasn t enough the method approach only allows static inheritance. Adding new methods to will not automatically expose _super in same named methods on . To get that working we would have to implement some kind of helper function to add methods that would add the enclosing function that sets up _super. In any case the result would be less than elegant and would introduce a possibly significant performance overhead. I hope you never use this function JavaScript has better patterns in store. If anything I think the _super implementation is a testament to JavaScript s flexibility. JavaScript doesn t have classes but it gives you the tools you need to build them should you need to do so. A _super Helper Function A somewhat saner implementation although not as concise can be achieved by implementing _super as a helper function piggybacking the prototype link as seen in Listing . Download from 144 Objects and Prototypal Inheritance Listing A simpler super implementation function _super object methodName var method methodName if typeof method function return Remove the first two arguments object and method var args .

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.