[英]Why can we use undefined object in Javascript function declaration?
Chromium WebUI has a cr.js file, which offers cr.define( name, fun )
method, for modulization. Chromium WebUI有一个cr.js文件,该文件提供了
cr.define( name, fun )
方法进行调制。 All the porperties of the object that fun
returns, will be added to the name
namescope object. 该对象的所有调性质是
fun
的回报,将被添加到该name
名称范围对象。
In chromium browser bookmarks manager page(chrome://bookmarks), a Javascript source file named bmm.js
is loaded. 在Chrome浏览器书签管理器页面(chrome:// bookmarks)中,加载了名为
bmm.js
的Javascript源文件。 This is a Javascript module in bookmark manager. 这是书签管理器中的Javascript模块。
Below is part of the bmm.js file, using cr.define()
, which defines a lot of methods, then add them to the bmm
object. 以下是使用
cr.define()
的bmm.js文件的一部分,该文件定义了许多方法,然后将它们添加到bmm
对象中。
cr.define('bmm', function() {
'use strict';
function contains(parent, descendant) {
if (descendant.parentId == parent.id)
return true;
// the bmm.treeLookup contains all folders
var parentTreeItem = bmm.treeLookup[descendant.parentId];
if (!parentTreeItem || !parentTreeItem.bookmarkNode)
return false;
return this.contains(parent, parentTreeItem.bookmarkNode);
}
// ... more function definitions ...
return {
contains: contains,
isFolder: isFolder,
loadSubtree: loadSubtree,
loadTree: loadTree,
addBookmarkModelListeners: addBookmarkModelListeners
};
});
The 2nd argument of cr.define();
cr.define();
的第二个参数cr.define();
is an anonymous function expression, it is parsed and executed, the returned value is an object, which is then passed to cr.define()
as argument. 是一个匿名函数表达式,将对其进行解析和执行,返回的值是一个对象,然后将其作为参数传递给
cr.define()
。
Notice in function declaration of function contains()
, there uses bmm
object, which should be undefined at the time this function body is being parsed. 请注意,在函数
function contains()
函数声明中,使用了bmm
对象,该对象在解析此函数主体时应未定义。
Why can we use bmm
and its method treeLookup
before they are defined ? 为什么在定义
bmm
及其方法treeLookup
之前可以使用它们?
Does function body of contains() is parsed and saved as a syntax tree, within which many nodes are tokens=undefined? 是否对contains()的函数体进行了解析并保存为语法树,其中的许多节点都是tokens = undefined?
It can not be parsed and saved in raw source code, because that means it is not parsed at all. 无法对其进行解析并将其保存在原始源代码中,因为这意味着根本就不会对其进行解析。
The 2nd argument of
cr.define()
is an anonymous function expression, it is parsed and executed, the returned value is an object, which is then passed tocr.define()
as argument.cr.define()
的第二个参数是一个匿名函数表达式,将对其进行解析和执行,返回值是一个对象,然后将其作为参数传递给cr.define()
。
In fact it's not. 实际上不是。 It's just passed as a function to
cr.define
, without being called. 它只是作为函数传递给
cr.define
,而没有被调用。 It's executed from within there : 它是从那里执行的:
var exports = fun();
Notice in function declaration of
function contains()
, there usesbmm
object, which should be undefined at the time this function body is being parsed.请注意,在函数
function contains()
函数声明中,使用了bmm
对象,该对象在解析此函数主体时应未定义。
Yes. 是。 And that doesn't matter at all.
这一点都不重要。 It's just a free variable with the name
bmm
. 这只是一个名为
bmm
的自由变量。
It will be resolved only when the function is called. 仅在调用该函数时才能解决。
Why can we use
bmm
and its methodtreeLookup
before they are defined?为什么在定义
bmm
及其方法treeLookup
之前可以使用它们?
They are not used - contains
is not called. 它们不被使用-
contains
不被调用。 They're just identifiers at parse time. 它们只是解析时的标识符。 The method call will be dispatched dynamically only when the code is actually executed.
仅当实际执行代码时,才会动态分派方法调用。
Is the function body of
contains()
parsed and saved as a syntax tree?contains()
的函数体是否已解析并保存为语法树?
Yes. 是。 That's what parsing does.
这就是解析的作用。
within which many nodes are tokens=undefined?
令牌=未定义在哪个节点内?
No. A syntax tree does not store values. 否。语法树不存储值。 The records for storing those are created at runtime, as are the values themselves.
用于存储这些记录的记录以及值本身都是在运行时创建的。 Only then assignments and lookups will take place.
只有这样才能进行分配和查找。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.