问题描述
function a(){
函数b(){
alert('boo')
}
var c ='绑定到本地调用对象'
d ='绑定到全局对象'
}
在这个例子中,试验指出b在a体外是不可达的,像c是。但是,d是 - 执行完一个()后。请在,我没有找到我正在寻找的确切措辞; Sec.13 p.71没有说的是函数声明语句创建的函数对象要绑定到哪个对象。我错过了什么?
这是静态的范围界定。函数中的语句在该函数中作用域。
但是,Javascript具有一种古怪的行为,即没有 var 关键字,就意味着一个全局变量 STRONG>。这就是你在测试中看到的。你的d变量是可用的,因为它是一个隐含的全局变量,尽管写在函数体内。
另外,要回答你的问题的第二部分:一个函数存在于它声明的任何范围内,就像变量一样。 旁注:
您可能不希望全局变量,尤其不是隐含的变量。建议您始终使用var关键字,以防止混淆并保持一切清洁。
旁注:
ECMA标准不是找到有关Javascript的答案的最有用的地方,但它肯定不是一个不好的资源。请记住,浏览器中的JavaScript只是该标准的一个实现,因此标准文档将为您提供当构建javascript引擎时实现者(大部分)遵循的规则。它无法提供关于您关心的实现的具体信息,即主要浏览器。有几本特别的书将会给你提供关于主要浏览器中的javascript实现如何表现的非常直接的信息。为了说明不同之处,我将在ECMAScript规范和Javascript一书中加入以下摘录。我认为您会同意本书给出更直接的答案。
以下是 ECMAScript语言规范:
以下是 O'Reilly's :
道格拉斯克罗克福德的书中强烈建议用于解决这类问题:
,同样来自O'Reilly。
Today I had a discussion with a colleague about nested functions in Javascript:
function a() {
function b() {
alert('boo')
}
var c = 'Bound to local call object.'
d = 'Bound to global object.'
}
In this example, trials point out that b is not reachable outside the body of a, much like c is. However, d is - after executing a(). Looking for the exact definition of this behaviour in the ECMAScript v.3 standard , I didn't find the exact wording I was looking for; what Sec.13 p.71 does not say, is which object the function object created by the function declaration statement is to be bound to. Am I missing something?
This is static scoping. Statements within a function are scoped within that function.
Javascript has a quirky behavior, however, which is that without the var keyword, you've implied a global variable. That's what you're seeing in your test. Your "d" variable is available because it is an implied global, despite being written within the body of a function.
Also, to answer the second part of your question: A function exists in whatever scope it is declared, just like a variable.
Sidenote:You probably don't want global variables, especially not implied ones. It's recommended that you always use the var keyword, to prevent confusion and to keep everything clean.
Sidenote:The ECMA Standard isn't probably the most helpful place to find answers about Javascript, although it certainly isn't a bad resource. Remember that javascript in your browser is just an implementation of that standard, so the standards document will be giving you the rules that were (mostly) followed by the implementors when the javascript engine was being built. It can't offer specific information about the implementations you care about, namely the major browsers. There are a couple of books in particular which will give you very direct information about how the javascript implementations in the major browsers behave. To illustrate the difference, I'll include excerpts below from both the ECMAScript specification, and a book on Javascript. I think you'll agree that the book gives a more direct answer.
Here's from the ECMAScript Language Specification:
Here's from O'Reilly's Javascript: The Definitive Guide (5th Edition):
Highly recommended for covering these kinds of questions is Douglas Crockford's book:
JavaScript, The Good Parts http://oreilly.com/catalog/covers/9780596517748_cat.gif
Javascript, The Good Parts, also from O'Reilly.
这篇关于Javascript / ECMAScript中函数的作用域是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!