Chromium WebUI has a cr.js file, which offers cr.define( name, fun ) method, for modulization. All the porperties of the object that fun returns, will be added to the name namescope object.
In chromium browser bookmarks manager page(chrome://bookmarks), a Javascript source file named bmm.js is loaded.
This is a Javascript module in bookmark manager.
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', 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(); 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.
Notice in function declaration of function contains(), there uses bmm object, which should be undefined at the time this function body is being parsed.
Why can we use bmm and its method treeLookup before they are defined ?
Does function body of contains() is parsed and saved as a syntax tree, within which many nodes are tokens=undefined?
It can not be parsed and saved in raw source code, because that means it is not parsed at all.
cr.defineas an argument.