简体   繁体   English

为什么在Javascript函数声明中使用未定义的对象?

[英]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 to cr.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 uses bmm 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 method treeLookup 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.

相关问题 函数声明未定义-为什么? - Function declaration undefined - Why? JavaScript - 为什么在函数表达式“未定义”中创建此函数声明? - JavaScript - Why is this function declaration created in a function expression "undefined"? 为什么我们不能立即调用 function 声明? - Why can't we immediately invoke function declaration? 我们可以使用 object 作为 Javascript 中的一个 object 的密钥吗? - Can we use object as a key of an object in Javascript? 为什么在函数内部我们可以覆盖“ undefined”,但不能在JS的window对象中使用…示例如下 - why inside function we can override “undefined” but can't in window object in JS…Example is given Below 为什么以及何时我们在javascript中使用对象分解? - why and when we use object destructuring in javascript? 为什么我们不能像在javascript中为对象常量那样向函数体内的函数对象添加属性? - Why can't we add properties to a function object inside function body like we do for object literals in javascript? 我们可以在javascript中使用函数作为返回语句吗? - Can we use function as a return statement in javascript? 我们可以同时使用构造函数作为 object 和 function 吗? - can we use a constructor as an object and function simultanously? 为什么我们需要Function.prototype.call,在这里我们可以通过将功能附加到javascript中的对象来实现相同的目的 - why we need Function.prototype.call , where we can achieve same thing by appending the function to object in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM