<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对象的方法不止这些。
分享到:
相关推荐
作用域 (Scope) 作用域定义了变量和过程在程序中的可见性和生命周期。 - **局部变量**:在过程或函数内部声明,只在该过程或函数内有效。 - **全局变量**:在程序顶部声明,整个程序中均可访问。 #### 13. 单元 ...
// a scope object cannot be injected into a service. }]); ``` 正确的方法是避免在服务中注入$scope,而是通过其他方式在控制器和服务之间进行通信。以下是几种正确的通信方式示例: 1. 使用回调函数: ```...
WSContext rs = (WSContext)call.invoke(new Object[]{"am", "", "eas", "bos80demo", "l2", Integer.valueOf(0)}); System.out.println(rs.getSessionId()); // 清理操作 call.clearOperation(); // 调用...
public void connect(IConnection conn, IScope scope, Object[] args) { this.conn = conn; this.scope = scope; // 其他逻辑 } } ``` - **开始录音录像:** ```java public String startRecord(String ...
Object result = context.callFunction(scope, scope, "add", new Object[]{5, 3}); System.out.println("Result: " + Context.toString(result)); } finally { Context.exit(); } } } ``` 这段代码创建了一个...
例如,`Object.prototype.toString.call([])`会返回`"[object Array]"`,这样就可以区分数组和其他类型的对象。 开发者工具也是获取类型信息的重要工具。Chrome DevTools、Firefox Developer Tools等提供了丰富的...
- **活动对象(Call Object)**:在执行环境中,活动对象保存了函数的参数、局部变量和函数声明。 - **作用域(Scope)**:指的是变量的可见范围,决定了变量在哪些地方可以被访问。 - **作用域链(Scope Chain)**...
call.invoke(new Object[]{1000}); ``` #### 结论 使用Axis 1.4开发WebService,涉及环境搭建、服务代码编写、部署描述符配置、服务部署及客户端调用等多个步骤。掌握这些流程,有助于开发者构建和维护基于SOAP...
Object result = function.call(rhino, scope, scope, functionParams); return Context.toString(result); } } finally { Context.exit(); } return null; } // JavaScript 调用的 Java 方法示例 ...
rtcInternal.h - RTC scope globals, defines, macros, and function declarations rtcsetup.cpp - Add IIDs for the RTC COM interfaces into the Registry rtcsetup.h - .. Session.cpp - RTC Session wrapper...
The default behavior of this method is to call addCookie(Cookie cookie) on the wrapped response object. addCookie(Cookie) - Method in interface javax.servlet.http.HttpServletResponse Adds the ...
GlobalScope.launch {val lebonCoinApiInterface = ApiRetrofitBuilder.build()lebonCoinApiInterface.getAllProducts().enqueue(object:retrofit2.Callback <List> {重写fun onFailure(call:Call <...
upsearch until a device scope is found before executing _ADR. This allows PCI_Config operation regions to be declared locally within control methods underneath PCI device objects. Fixed a problem ...
int totalVolume = (Integer) call.invoke(new Object[]{}, url); System.out.println("Total Volume: " + totalVolume); } } ``` 在这个客户端示例中,首先设置目标服务的URI,接着通过`addParameter`方法将...
2. **执行函数**:调用`a()`时,创建了函数作用域`a.scope=a`,并且创建了活动对象`callObject`,将其放在作用域链的顶端。此时,`a`的作用域链包括`a`和`window`两个对象。 3. **添加arguments属性**:在活动对象`...
为了调用一个Lua函数, 你可以或者用 call_function() 或者用 一个对象(object). template Ret call_function(lua_State* L, const char* name, ...) template Ret call_function(object const& obj, ...) call_...
When we have a closely related data of the same type and scope, it is better to declare it in an array. Multidimensional array java A two dimensional array can be thought as a grid of rows and ...
2. **活动对象(Call Object)**:每个执行上下文都有一个活动对象,存储当前上下文中的变量和函数声明。 3. **作用域(Scope)**:作用域决定了变量的可见性,它是由函数定义的位置决定的,决定了变量在哪一层级的...
String result = (String) call.invoke(new Object[]{"ml1", "ml2"}); System.err.println(result); } catch (ServiceException e) { // 处理异常 } catch (RemoteException e) { // 处理异常 } } } ``` ...