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

Tham khảo tài liệu 'phát triển javascript - part 29', 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 an XMLHttpRequest Object 253 will do as again ships with IE6 includes the alias for backwards compatibility. Implementing With knowledge of the different objects available we can take a shot at implementing as seen in Listing . Listing Creating an XMLHttpRequest object ajax .create function var options function return new ActiveXObject function return new XMLHttpRequest for var i 0 l i l i try return options i catch e return null Running the tests confirms that our implementation is sufficient. First test green Before we hasten on to the next test we should look for possible duplication and other areas that could be improved through refactoring. Although there is no obvious duplication in code there is already duplication in execution the try catch to find a suitable object is executed every time an object is created. This is wasteful and we can improve the method by figuring out which object is available before defining it. This has two benefits The call time overhead is eliminated and feature detection becomes built-in. If there is no matching object to create then there will be no which means that client code can simply test for its existence to determine if XMLHttpRequest is supported by the browser. Listing improves the method. Download from 254 Abstracting Browser Differences Ajax Listing Checking for support upfront function var xhr var ajax ajax var options . Same as before for var i 0 l i l i try xhr options i options i break catch e With this implementation in place the try catch will only run at load time. If successfully created will call the correct function directly. The test still runs green so we can focus on the next requirement. Stronger Feature Detection The test we just wrote is .

TỪ KHÓA LIÊN QUAN
Đã 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.