我无法理解“简化 JavaScript 中的函数是具有词法作用域的 lambda。”

发布于 2024-11-08 15:07:35 字数 854 浏览 0 评论 0原文

我正在读 Douglas Crockford 的书《JavaScript:The Good Parts》。我无法理解“简化 JavaScript 中的函数是具有词法作用域的 lambda”这句话。这句话想告诉我们什么?这句话怎么理解呢?

非常感谢!

以下是书中这句话的上下文:

我想包含所有代码 JavaScript 中的解析器可以解析 JavaScript。但我的章节只是 30 或 40 之一,所以我感到受到限制 我可以的页数 消耗。进一步的复杂化是 我的大多数读者都没有 有 JavaScript 经验,所以我也 必须介绍语言 及其特点。

因此,我决定对语言进行子集化。 这样,我就不必解析 整个语言,我不必 描述整个语言。我打了电话 简化 JavaScript 的子集。 选择子集很容易:它 只包括我认为的功能 需要编写一个解析器。就是这样 我在《美丽的代码》中对此进行了描述:

简化的 JavaScript 就是好的 东西,包括:

作为 firortranst 类对象的函数

简化 JavaScript 中的函数是具有词法作用域的 lambda。

具有原型的动态对象 继承

对象是无类的。我们可以添加一个 通过普通方式为任何对象添加新成员 任务。对象可以继承 来自另一个对象的成员。

对象文字和数组文字

这是一个非常方便的表示法 创建新的对象和数组。 JavaScript 文字是 JSON 数据的灵感 交换格式。

I'm reading Douglas Crockford's book "JavaScript: The Good Parts". I can't understand the sentence that "Functions in Simplified JavaScript are lambdas with lexical scoping.". What does this sentence want to tell us? How do you understand this sentence?

Thank you very much!

Below is the contexts of this sentence in the book:

I wanted to include all of the code
for a parser in JavaScript that parses
JavaScript. But my chapter was just
one of 30 or 40, so I felt constrained
in the number of pages I could
consume. A further complication was
that most of my readers would have no
experience with JavaScript, so I also
would have to introduce the language
and its peculiarities.

So, I decided to subset the language.
That way, I wouldn't have to parse the
whole language, and I wouldn't have to
describe the whole language. I called
the subset Simplified JavaScript.
Selecting the subset was easy: it
included just the features that I
needed to write a parser. This is how
I described it in Beautiful Code:

Simplified JavaScript is just the good
stuff, including:

Functions as firortranst class objects

Functions in Simplified JavaScript are lambdas with lexical scoping.

Dynamic objects with prototypal
inheritance

Objects are class-free. We can add a
new member to any object by ordinary
assignment. An object can inherit
members from another object.

Object literals and array literals

This is a very convenient notation for
creating new objects and arrays.
JavaScript literals were the
inspiration for the JSON data
interchange format.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

殊姿 2024-11-15 15:07:35

他试图解释 JavaScript 中函数声明的本质。通过将它们描述为“具有词法作用域的 lambda”,他回避了它们的使用方式。例如,您可以静态地声明它们:

function myFunc(arg) {
  // do something
}

您可以将它们存储在变量中:

var myFunc = function(arg) {
  // do something
};

您可以将它们用作对象属性(方法):

var myObject = {
  myMethod: function (arg) {
    // do something
  }
};

或者您可以在将它们传递到某处时声明它们(在使用 jQuery 时很常见):

$('.myClass').click(function () {
  // do something when an element with class "myClass" is clicked.
});

因为声明时间/方式的灵活性,它们与 lambda 在许多其他语言(例如 Python)中扮演的角色相同。

He's attempting to explain the nature of function declarations in JavaScript. By describing them as "lambdas with lexical scoping" he's eluding to the number of ways they can be used. You can, for example, declare them statically:

function myFunc(arg) {
  // do something
}

You can store them in a variable:

var myFunc = function(arg) {
  // do something
};

You can use them as object properties (methods):

var myObject = {
  myMethod: function (arg) {
    // do something
  }
};

Or you can declare them as you pass them somewhere (very common when working with jQuery):

$('.myClass').click(function () {
  // do something when an element with class "myClass" is clicked.
});

Because of the flexibility in when / how they're declared they fill the same role that lambdas do in many other languages (Python for example).

站稳脚跟 2024-11-15 15:07:35

lambda 函数是匿名函数的另一个术语。他说 javascript 支持匿名函数(您可以动态创建函数而无需给它们命名,例如 var myVar = function () { ... } )。词法作用域是指 javascript 如何处理作用域。请参阅: http://en.wikipedia.org/wiki/Scope_%28computer_science%29 #Lexical_versus_dynamic_scoping

A lambda function is another term for an anonymous function. He is saying that javascript supports anonymous functions ( you can create functions on the fly without giving them names, such as var myVar = function () { ... } ). Lexical scoping is referring to how javascript handles scope. see: http://en.wikipedia.org/wiki/Scope_%28computer_science%29#Lexical_versus_dynamic_scoping

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文