You Don't Know JS: Scope & Closures(翻译的试试)

Chapter 1: What is Scope?

第一章:什么是作用域

One of the most fundamental paradigms of nearly all programming languages is the ability to store values in variables, and later retrieve or modify those values. In fact, the ability to store values and pull values out of variables is what gives a program state.

在几乎所有编程语言中,储存值到变量中和稍后检索或改变这些值都是最基本的能力之一。事实上,这种存储值到变量和将值移除变量的能力才给了程序以“状态”。

Without such a concept, a program could perform some tasks, but they would be extremely limited and not terribly interesting.

没了这个概念的话,一个程序也能完成一些任务,但他们就会有很多地方被限制并且一点都不有趣了。

But the inclusion of variables into our program begets the most interesting questions we will now address: where do those variables live? In other words, where are they stored? And, most importantly, how does our program find them when it needs them?

但是把变量包含到我们的程序中带来的最有趣的问题是:这些变量在哪儿?或者说,他们存储在哪儿?最重要的是,当我们的程序需要这些变量的时候,它是如何找到他们的?

These questions speak to the need for a well-defined set of rules for storing variables in some location, and for finding those variables at a later time. We‘ll call that set of rules: Scope.

这个问题需要一个定义明确的规则,用于存储变量在某些位置,以及在之后再找出这些变量,我们可以把这个规则叫:作用域

But, where and how do these Scope rules get set?

但是,作用域规则在哪儿设置?怎么设置的?

Compiler Theory

编译原理

It may be self-evident, or it may be surprising, depending on your level of interaction with various languages, but despite the fact that JavaScript falls under the general category of "dynamic" or "interpreted" languages, it is in fact a compiled language. It is not compiled well in advance, as are many traditionally-compiled languages, nor are the results of compilation portable among various distributed systems.

这可能是不言而喻的,或者可能令人惊讶——取决于你融会贯通各种语言的能力,但是尽管JS属于一般范畴的动态型和解释型语言,实际上它却是一个编译型语言。它不是提前编译好的,因为有许多传统编译语言也不是各种分布式编译系统的产物。

But, nevertheless, the JavaScript engine performs many of the same steps, albeit in more sophisticated ways than we may commonly be aware, of any traditional language-compiler.

但是,尽管如此,JS引擎也执行着许多相同的步骤,尽管它使用的是一种更为复杂的方式——比我们可能意识到的任何传统编译语言

In traditional compiled-language process, a chunk of source code, your program, will undergo typically three steps before it is executed, roughly called "compilation":

在传统的编译语言过程中,一块源代码,也就是你的程序,会在执行前遵从典型的‘三步走’,也就是‘编译’

You Don't Know JS: Scope & Closures(翻译的试试)

时间: 2024-07-30 03:02:15

You Don't Know JS: Scope & Closures(翻译的试试)的相关文章

You Don't Know JS: Scope & Closures (第3章: 函数 vs 块作用域)

第二章,作用域由一系列的bubbles组成.每一个都代表了一个container或bucket,装着被声明的identifiers(variables, functions).这些bubbles相互嵌套.这种嵌套是在开发阶段写完的. 什么制造了一个新的bubble? 只是函数吗?其他的JS结构可以创建bubbles of scope吗? Function Vs. Block Scope Scope From Functions 探索函数作用域和它的暗示implications. 函数作用域内的所

You Don't Know JS: Scope & Closures (第4章: Hoisting)

Chapter4: Hoisting 变量附加到哪个层次的scope,由它们在哪里和如何声明(let, var)来决定. Function scope/Block scope都有相同的法则:任何变量在一个scope内声明,则这个变量附加到这个作用域上. 但有一个细节问题:当声明declarations出现在一个作用域中的不同的位置的时候,scope附加如何与declarations协作? Chicken or The Egg? temptation: a strong desire to hav

You Don't Know JS: Scope & Closures (附加:Lexical/dynamic作用域)

JavaScript只有Lexical Scope 模式 Lexical Scope就是在写代码的时候,定义函数的时候创建的作用域! 而动态作用域是在runtime时,函数被调用的地方的作用域! 实际上 dynamic Scope是 this关键字的近亲.这会在this & Object Prototypes系列讲解. 第二章,lexical scope是这方面的法则:Engine如何查询一个变量,在哪找到这个变量! 关键特征就是lexical scope是定义在author-time,打代码的

网页3D引擎“Babylon.JS”入门教程翻译总结

使用三个月的业余时间把官方教程的入门部分译为中文并上传到github,在下一步编程前做一个总结. 历程: 最早接触游戏编程是在大三下学期,用汇编语言和实验室里的单片机.触摸屏.电机(提供声效)编的打地鼠程序.后来因为大四的毕业设计与三维空间模拟有关,又自学了MFC编程和NEHE的OpenGL教程(国人翻译版)(C++3D编程的复杂会给任何参与者留下深刻的印象,向先驱者们致敬).工作后自学了JavaScript语言和前端知识,感觉js与C++相比极其简单易用,如果能以js代替C++进行3D开发可以

js通过google翻译插件实现多语言版本

目前谷歌翻译小工具对中文网页(以及其他语言网页)提供了对其他二十三种语言的翻译: 中文(繁体),英文,阿拉伯文,保加利亚文,波兰语,朝鲜语,丹麦语,德语,俄语,法语,芬兰语,荷兰语,捷克语,克罗地亚文,罗马尼亚语,挪威语,葡萄牙语,日语,瑞典语,西班牙语,希腊语,意大利语和印度文.也就是说当你的网站使用了谷歌翻译小工具后,你的网站马上可以以另外二十三种语言显示,这对所有期望与国际接轨,让全球网民了解中国博大精深的网页资讯的有志站主来说都是一大福音.当然,机器自动翻译的翻译质量还远远落后于专业但昻

浅谈JS变量声明和函数声明提升

先来两个问题 很多时候,在直觉上,我们都会认为JS代码在执行时都是自上而下一行一行执行的,但是实际上,有一种情况会导致这个假设是错误的. a = 2; var a; console.log(a); 按照传统眼光,console.log(a)输出的应该是undefined,因为var a在a = 2之后.但是,输出的是2. 再看第二段代码: console.log(a); var a = 2; 有人会想到第一段代码,然后回答undefined.还有人会认为a在使用前未被声明,因此抛出Referen

[it-ebooks]电子书列表

#### it-ebooks电子书质量不错,但搜索功能不是很好 #### 格式说明  [ ]中为年份      ||  前后是标题和副标题  #### [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Objective-C to develop iPhone games http://it-ebooks.info/book/3544/ Learning Web App Developmen

(转) [it-ebooks]电子书列表

[it-ebooks]电子书列表 [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Objective-C to develop iPhone games http://it-ebooks.info/book/3544/Learning Web App Development || Build Quickly with Proven JavaScript Techniques http://

Angular this vs $scope

this vs $scope ------------------------------------------------------------------------------ 'this' vs $scope in AngularJS controllers How does this and $scope work in AngularJS controllers? Short answer: this When the controller constructor functio