scope, call object

本文详细解释了JavaScript中的作用域概念及其内部表示形式——调用对象,并通过示例介绍了闭包的使用方式。同时,文章还阐述了ECMA-262规范中关于内置对象的定义。

<noscript type="text/javascript" event="FSCommand(info,args)" for="sIFR_callback_2">sIFR_callback_2_DoFSCommand(info, args);</noscript>Scope, the scope chain and closures

But before we get into closures, something about scope. Every JavaScript expression is surrounded in a scope. The scope contains what is available to the expression. When you define a new function, the body of this function exists in a new scope. For example:

function foo(){
    var msg = "hello world";
    alert(msg);
};

foo();

Here alert(msg); works, because msg is defined in the same scope as in which alert is called.

Deep down in the JavaScript interpreter the scope is represented as a call object. This object contains all the variables which have been defined in the scope that the call object represents. When a new scope is created, it's call object is linked to the call object of the scope it was created in. This creates a scope chain.

//我的理解:scope = call object    

This phenomenon is best explained through an example. In JavaScript the window object is the global scope. This is the highest scope in the scope chain. If we create a new function in the global scope the scope of this function will be chained to the window object:

var msg = "hello world";

function foo(){
    alert(msg);
};

foo();

Now, if you call foo, it'll alert hello world! But how does it know the value of msg, which wasn’t defined in the function body foo? It turns out that if the JavaScript interpreter can’t find a variable in it’s current scope, it’ll go up the scope chain and searches the parent scope until it finds the variable. In our case it finds msg in global scope.

You can also create a scope chain by nesting functions:

function foo(){
    var msg = "hello world";
    function bar(){
        alert(msg);
    };
    bar();
};

foo();

Now, if you call foo, it’ll define bar and execute it immediately afterwards. And as expected it’ll alert hello world.

When a nested function has access to the scope of it’s parent function, it is called a closure. sIFR relies quite heavily on closures. 

 ------------------------------------------------------

ECMA-262把内置对象(built-in object)定义为由ECMAScript实现提供的、独立于宿主环境的所有对象,在ECMAScript程序开始执行时出现。
这意味着开发者不必明确实例化内置对象,它已被实例化了。ECMA-262只定义了两个内置对象,即Global和Math(它们也是本地对象,根据定义,每个内置对象都是本地对象)。

1. Global对象
Global对象是ECMAScript中最特别的对象,因为实际上它根本不存在。如果尝试编写下面的代码,将得到错误:
var pointer = Global;
错误消息显示Global不是对象,但刚才不是说Global是对象吗?没错。这里需要理解的主要概念是,在ECMAScript中,不存在独立的函数,
所有函数都必须是某个对象的方法。本书前面介绍的函数,如isNaN()、isFinite()、parseInt()和parseFloat()等,看起来都像独立的函数。
实际上,它们都是Global对象的方法。而且Global对象的方法不止这些。
       

 

Exception Group Traceback (most recent call last): | | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\starlette\_utils.py", line 76, in collapse_excgroups | yield | | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\starlette\middleware\base.py", line 186, in __call__ | async with anyio.create_task_group() as task_group: | │ │ └ <anyio._backends._asyncio.TaskGroup object at 0x00000168B3F1F4F0> | │ └ <function create_task_group at 0x00000168A54857E0> | └ <module 'anyio' from 'D:\\dev\\EquipmentRegistryDataPlatform\\venv\\lib\\site-packages\\anyio\\__init__.py'> | | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\anyio\_backends\_asyncio.py", line 815, in __aexit__ | raise BaseExceptionGroup( | └ <class 'exceptiongroup.BaseExceptionGroup'> | | exceptiongroup.ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception) +-+---------------- 1 ---------------- | Traceback (most recent call last): | | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 403, in run_asgi | result = await app( # type: ignore[func-returns-value] | └ <uvicorn.middleware.proxy_headers.ProxyHeadersMiddleware object at 0x00000168A4F34760> | | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 60, in __call__ | return await self.app(scope, receive, send) | │ │ │ │ └ <bound method RequestResponseCycle.send of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x00000168B3F1E860>> | │ │ │ └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x00000168B3F1E... | │ │ └ {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.3'}, 'http_version': '1.1', 'server': ('172.23.49.43', 9999), ... | │ └ <fastapi.applications.FastAPI object at 0x00000168B3AF1D50> | └ <uvicorn.middleware.proxy_headers.ProxyHeadersMiddleware object at 0x00000168A4F34760> | | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\fastapi\applications.py", line 1054, in __call__ | await super().__call__(scope, receive, send) | │ │ └ <bound method RequestResponseCycle.send of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x00000168B3F1E860>> | │ └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x00000168B3F1E... | └ {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.3'}, 'http_version': '1.1', 'server': ('172.23.49.43', 9999), ... | | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\starlette\applications.py", line 113, in __call__ | await self.middleware_stack(scope, receive, send) | │ │ │ │ └ <bound method RequestResponseCycle.send of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x00000168B3F1E860>> | │ │ │ └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x00000168B3F1E... | │ │ └ {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.3'}, 'http_version': '1.1', 'server': ('172.23.49.43', 9999), ... | │ └ <starlette.middleware.errors.ServerErrorMiddleware object at 0x00000168B3D038E0> | └ <fastapi.applications.FastAPI object at 0x00000168B3AF1D50> | | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\starlette\middleware\errors.py", line 187, in __call__ | raise exc | | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\starlette\middleware\errors.py", line 165, in __call__ | await self.app(scope, receive, _send) | │ │ │ │ └ <function ServerErrorMiddleware.__call__.<locals>._send at 0x00000168A65497E0> | │ │ │ └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x00000168B3F1E... | │ │ └ {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.3'}, 'http_version': '1.1', 'server': ('172.23.49.43', 9999), ... | │ └ <starlette.middleware.cors.CORSMiddleware object at 0x00000168B3D038B0> | └ <starlette.middleware.errors.ServerErrorMiddleware object at 0x00000168B3D038E0> | | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\starlette\middleware\cors.py", line 93, in __call__ | await self.simple_response(scope, receive, send, request_headers=headers) | │ │ │ │ │ └ Headers({'host': '172.23.49.43:9999', 'connection': 'keep-alive', 'content-length': '117', 'pragma': 'no-cache', 'cache-contr... | │ │ │ │ └ <function ServerErrorMiddleware.__call__.<locals>._send at 0x00000168A65497E0> | │ │ │ └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x00000168B3F1E... | │ │ └ {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.3'}, 'http_version': '1.1', 'server': ('172.23.49.43', 9999), ... | │ └ <function CORSMiddleware.simple_response at 0x00000168B3AF7490> | └ <starlette.middleware.cors.CORSMiddleware object at 0x00000168B3D038B0> | | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\starlette\middleware\cors.py", line 144, in simple_response | await self.app(scope, receive, send) | │ │ │ │ └ functools.partial(<bound method CORSMiddleware.send of <starlette.middleware.cors.CORSMiddleware object at 0x00000168B3D038B0... | │ │ │ └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x00000168B3F1E... | │ │ └ {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.3'}, 'http_version': '1.1', 'server': ('172.23.49.43', 9999), ... | │ └ <app.core.middlewares.BackGroundTaskMiddleware object at 0x00000168B3D03850> | └ <starlette.middleware.cors.CORSMiddleware object at 0x00000168B3D038B0> | | File "D:\dev\EquipmentRegistryDataPlatform\app\core\middlewares.py", line 28, in __call__ | await self.handle_http(scope, receive, send) | │ │ │ │ └ functools.partial(<bound method CORSMiddleware.send of <starlette.middleware.cors.CORSMiddleware object at 0x00000168B3D038B0... | │ │ │ └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x00000168B3F1E... | │ │ └ {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.3'}, 'http_version': '1.1', 'server': ('172.23.49.43', 9999), ... | │ └ <function SimpleBaseMiddleware.handle_http at 0x00000168B3B141F0> | └ <app.core.middlewares.BackGroundTaskMiddleware object at 0x00000168B3D03850> | | File "D:\dev\EquipmentRegistryDataPlatform\app\core\middlewares.py", line 38, in handle_http | await response(scope, receive, send_wrapper) | │ │ │ └ <function SimpleBaseMiddleware.handle_http.<locals>.send_wrapper at 0x00000168A6549A20> | │ │ └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x00000168B3F1E... | │ └ {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.3'}, 'http_version': '1.1', 'server': ('172.23.49.43', 9999), ... | └ <app.core.middlewares.APILoggerMiddleware object at 0x00000168B3D037F0> | | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\starlette\middleware\base.py", line 185, in __call__ | with collapse_excgroups(): | └ <function collapse_excgroups at 0x00000168A56A4940> | | File "C:\Program Files\Python310\lib\contextlib.py", line 153, in __exit__ | self.gen.throw(typ, value, traceback) | │ │ │ │ │ └ <traceback object at 0x00000168B523ABC0> | │ │ │ │ └ ExceptionGroup('unhandled errors in a TaskGroup', [TypeError("Unable to apply constraint 'max_length' to supplied value 2025-... | │ │ │ └ <class 'exceptiongroup.ExceptionGroup'> | │ │ └ <method 'throw' of 'generator' objects> | │ └ <generator object collapse_excgroups at 0x00000168B507E1F0> | └ <contextlib._GeneratorContextManager object at 0x00000168B3F1E830> | | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\starlette\_utils.py", line 82, in collapse_excgroups | raise exc | | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\starlette\middleware\base.py", line 187, in __call__ | response = await self.dispatch_func(request, call_next) | │ │ │ └ <function BaseHTTPMiddleware.__call__.<locals>.call_next at 0x00000168A6549990> | │ │ └ <starlette.middleware.base._CachedRequest object at 0x00000168B3F1E950> | │ └ <bound method APILoggerMiddleware.dispatch of <app.core.middlewares.APILoggerMiddleware object at 0x00000168B3D037F0>> | └ <app.core.middlewares.APILoggerMiddleware object at 0x00000168B3D037F0> | | File "D:\dev\EquipmentRegistryDataPlatform\app\core\middlewares.py", line 103, in dispatch | response = await call_next(request) | │ └ <starlette.middleware.base._CachedRequest object at 0x00000168B3F1E950> | └ <function BaseHTTPMiddleware.__call__.<locals>.call_next at 0x00000168A6549990> | | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\starlette\middleware\base.py", line 163, in call_next | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\starlette\_utils.py", line 82, in collapse_excgroups | raise exc | | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\starlette\middleware\base.py", line 187, in __call__ | response = await self.dispatch_func(request, call_next) | │ │ │ └ <function BaseHTTPMiddleware.__call__.<locals>.call_next at 0x00000168A6549990> | │ │ └ <starlette.middleware.base._CachedRequest object at 0x00000168B3F1E950> | │ └ <bound method APILoggerMiddleware.dispatch of <app.core.middlewares.APILoggerMiddleware object at 0x00000168B3D037F0>> | └ <app.core.middlewares.APILoggerMiddleware object at 0x00000168B3D037F0> | | File "D:\dev\EquipmentRegistryDataPlatform\app\core\middlewares.py", line 103, in dispatch | response = await call_next(request) | │ └ <starlette.middleware.base._CachedRequest object at 0x00000168B3F1E950> | └ <function BaseHTTPMiddleware.__call__.<locals>.call_next at 0x00000168A6549990> | | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\starlette\middleware\base.py", line 163, in call_next | raise exc | | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\starlette\middleware\base.py", line 187, in __call__ | response = await self.dispatch_func(request, call_next) | │ │ │ └ <function BaseHTTPMiddleware.__call__.<locals>.call_next at 0x00000168A6549990> | │ │ └ <starlette.middleware.base._CachedRequest object at 0x00000168B3F1E950> | │ └ <bound method APILoggerMiddleware.dispatch of <app.core.middlewares.APILoggerMiddleware object at 0x00000168B3D037F0>> | └ <app.core.middlewares.APILoggerMiddleware object at 0x00000168B3D037F0> | | File "D:\dev\EquipmentRegistryDataPlatform\app\core\middlewares.py", line 103, in dispatch | response = await call_next(request) | │ └ <starlette.middleware.base._CachedRequest object at 0x00000168B3F1E950> | └ <function BaseHTTPMiddleware.__call__.<locals>.call_next at 0x00000168A6549990> | | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\starlette\middleware\base.py", line 163, in call_next | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\starlette\middleware\base.py", line 187, in __call__ | response = await self.dispatch_func(request, call_next) | │ │ │ └ <function BaseHTTPMiddleware.__call__.<locals>.call_next at 0x00000168A6549990> | │ │ └ <starlette.middleware.base._CachedRequest object at 0x00000168B3F1E950> | │ └ <bound method APILoggerMiddleware.dispatch of <app.core.middlewares.APILoggerMiddleware object at 0x00000168B3D037F0>> | └ <app.core.middlewares.APILoggerMiddleware object at 0x00000168B3D037F0> | | File "D:\dev\EquipmentRegistryDataPlatform\app\core\middlewares.py", line 103, in dispatch | response = await call_next(request) | │ └ <starlette.middleware.base._CachedRequest object at 0x00000168B3F1E950> | └ <function BaseHTTPMiddleware.__call__.<locals>.call_next at 0x00000168A6549990> | | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\starlette\middleware\base.py", line 163, in call_next | │ │ └ <starlette.middleware.base._CachedRequest object at 0x00000168B3F1E950> | │ └ <bound method APILoggerMiddleware.dispatch of <app.core.middlewares.APILoggerMiddleware object at 0x00000168B3D037F0>> | └ <app.core.middlewares.APILoggerMiddleware object at 0x00000168B3D037F0> | | File "D:\dev\EquipmentRegistryDataPlatform\app\core\middlewares.py", line 103, in dispatch | response = await call_next(request) | │ └ <starlette.middleware.base._CachedRequest object at 0x00000168B3F1E950> | └ <function BaseHTTPMiddleware.__call__.<locals>.call_next at 0x00000168A6549990> | | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\starlette\middleware\base.py", line 163, in call_next | File "D:\dev\EquipmentRegistryDataPlatform\app\core\middlewares.py", line 103, in dispatch | response = await call_next(request) | │ └ <starlette.middleware.base._CachedRequest object at 0x00000168B3F1E950> | └ <function BaseHTTPMiddleware.__call__.<locals>.call_next at 0x00000168A6549990> | | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\starlette\middleware\base.py", line 163, in call_next | │ └ <starlette.middleware.base._CachedRequest object at 0x00000168B3F1E950> | └ <function BaseHTTPMiddleware.__call__.<locals>.call_next at 0x00000168A6549990> | | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\starlette\middleware\base.py", line 163, in call_next | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\starlette\middleware\base.py", line 163, in call_next | raise app_exc | └ TypeError("Unable to apply constraint 'max_length' to supplied value 2025-08-24 16:00:00+00:00") | | raise app_exc | └ TypeError("Unable to apply constraint 'max_length' to supplied value 2025-08-24 16:00:00+00:00") | | └ TypeError("Unable to apply constraint 'max_length' to supplied value 2025-08-24 16:00:00+00:00") | | File "D:\dev\EquipmentRegistryDataPlatform\venv\lib\site-packages\starlette\middleware\base.py", line 149, in coro | await self.app(scope, receive_or_disconnect, send_no_error) | │ │ │ │ └ <function BaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.send_no_error at 0x00000168A654AA70> | │ │ │ └ <function BaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.receive_or_disconnect at 0x00000168A6549900> | │ │ └ {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.3'}, 'http_version': '1.1', 'server': ('172.23.49.43', 9999), ... | │ └ <app.core.middlewares.APILoggerAddResponseMiddleware object at 0x00000168B3D03730> | └ <app.core.middlewares.APILoggerMiddleware object at 0x00000168B3D037F0> | | File "D:\dev\EquipmentRegistryDataPlatform\app\core\middlewares.py", line 28, in __call__ | await self.handle_http(scope, receive, send) | │ │ │ │ └ <function BaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.send_no_error at 0x00000168A654AA70> | │ │ │ └ <function BaseHTTPMiddleware.__call__.<locals>.call_next.<locals>.receive_or_disconnect at 0x00000168A654WARNING: StatReload detected changes in 'app\api\v1\zentao\zt_materiallib.py'. Reloading...
08-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值