简体   繁体   中英

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. 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.

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.

In fact it's not. It's just passed as a function to cr.define , without being called. It's executed from within there :

var exports = fun();

Notice in function declaration of function contains() , there uses bmm object, which should be undefined at the time this function body is being parsed.

Yes. And that doesn't matter at all. It's just a free variable with the name bmm .

It will be resolved only when the function is called.

Why can we use bmm and its method treeLookup before they are defined?

They are not used - contains is not called. 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM