Phát triển Javascript - part 12

Tham khảo tài liệu 'phát triển javascript - part 12', 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ả | Scope and Execution Context 83 In addition to the window property in browsers the global object can be accessed as this in the global scope. Listing shows how window relates to the global object in browsers. Listing The global object and window var global this TestCase GlobalObjectTest test window should be global object function assertSame global window assertSame window assertSame window In the global scope the global object is used as the variable object meaning that declaring variables using the var keyword results in corresponding properties on the global object. In other words the two assignments in Listing are almost equivalent. Listing Assigning properties on the global object var assert function . function . These two statements are not fully equivalent because the variable declaration is hoisted whereas the property assignment is not. The Scope Chain Whenever a function is called control enters a new execution context. This is even true for recursive calls to a function. As we ve seen the activation object is used for identifier resolution inside the function. In fact identifier resolution occurs through the scope chain which starts with the activation object of the current execution context. At the end of the scope chain is the global object. Consider the simple function in Listing . Calling it with a number results in a function that when called adds that number to its argument. Listing A function that returns another function function adder base return function num Download from 84 Functions return base num Listing uses adder to create incrementing and decrementing functions. Listing Incrementing and decrementing functions TestCase AdderTest test should add or subtract one from arg function var inc adder l var dec adder -l assertEquals 3 inc 2 assertEquals 3 dec 4 assertEquals 3 inc dec 3 The scope chain for the inc method contains its own .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
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.