函数声明优先/覆盖变量声明?吊装?为什么? [英] Function declarations precedence/overwriting variable declarations? Hoisting? Why?

查看:138
本文介绍了函数声明优先/覆盖变量声明?吊装?为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码段1:

var a; // undefined variable named 'a'
function a(foo) { // a function named 'a'
  var foo = "Hello World";
  console.log(foo);
}
console.log(a); // output is: [Function: a], but why not undefined?

代码段2:

function a(foo) { // a function named 'a'
    var foo = "Hello World";
    console.log(foo);
}
var a; // undefined variable named 'a'
console.log(a); // output is: [Function: a], but why not undefined?

我本可以仅向片段1提出这个问题-但是,出于完整性的考虑,我都给出了这两个片段.我也在其中写了一些简短的评论.

I could have just shown Snippet 1 to ask this question - however I showed both just for completeness purposes. I have wrote some brief comments in them as well.

我的问题是,在这两种情况下,为什么对变量声明使用函数声明"覆盖"/阴影"还是采用"优先级"?有人可以解释一下这种行为吗?

My question is, in both cases, why is the function declaration ‘overwriting’/’shadowing’ or taking ‘precedence’ over the variable declaration? Can someone please explain this behaviour?

我了解到,由于起重"现象,这些结果在最终结果方面可以说是相同的(从解释器的角度来看),但是为什么执行函数声明或变量声明在由Java脚本引擎解释/解析时(创建期间 阶段)是否比其他声明具有优先级?即哪一个悬挂在另一个之上?我已经读过,函数声明优先于变量声明-但是为什么是这种情况?

I understand that these arguably are the same (from the perspective of the interpreter) in terms of end-result, due to the ‘hoisting’ phenomenon, but why does the function declaration or the variable declaration have precedence over the other when it’s interpreted/parsed by the Javascript engine (during the creation phase)? i.e. which one gets hoisted above the other? I have read it is that the function declaration has precedence over the variable declaration - but why is this the case?

也请参考第一个代码段(但这在两种情况下都适用)是'a'的内存位置/地址,在第1行和第6行中声明了该位置相同?并且由于"a"在代码的两个位置中均表示函数声明,因此在解析第1行和第6行之后,其在存储位置/地址中的"a"的值将是多少?在创建阶段?由于函数声明被悬挂"在变量声明上方,这是否意味着在第一行的末尾,"a"的内存地址指向函数对象"a" . 阶段的执行上下文?

Also, kindly refer to the first code snippet (but this applies to both cases), is the memory location/address of ‘a’ where it’s declared in line 1, and line 6, EXACTLY the same? And since ‘a’, in both places of the code, represents the function declaration, what would be the value of ‘a’ at its memory location/address after parsing line 1 and line 6 during the creation phase? Since function declarations are 'hoisted' above the variable declaration, does that mean that by line 1, 'a's memory address is pointing to the function object 'a', by the end of the creation phase of the execution context?

在两个代码段中,'var a'是未定义的,因此为什么 不是' t 'a '到达 执行 阶段时的 undefined 值(它的执行点从第6行开始?) JavaScript引擎是否只是简单地说:啊,如果我们允许'var a''覆盖''function a(){…}',它将继续是未定义的,因此无法使用.而如果我们允许"a"代表所述功能,那么至少用户可以调用该功能(即使在其声明之前,也不会出现引用错误). ,也许吧?

In both code snippets, ‘var a’ is undefined, so why isnt ‘a’ the value of undefined by the time we reach the execution phase (which has its execution point starting on line 6?) Does the Javascript engine simply say, "Ah, if we allow ‘var a’ to ‘overwrite’ ‘function a() {…}’, it would continue to be undefined and thus be unusable. Whereas, if we allow ‘a’ to represent said function, then at least the user can invoke said function (even prior to its declaration, without getting a reference error)". This is very unlikely to be the case but sounds logical/ideal, maybe?

所以,我的问题是:

  1. 原因为何函数声明被覆盖或优先于变量声明?是因为它是ECMAScript规范的实现,我们只是接受表面上的东西吗?还是我们可以解释一下这种行为?
  1. What’s the reason behind why the function declaration is overwriting or having precedence over the variable declaration? Is it something we just accept prima-facie because it's an implementation of the ECMAScript specifications or can we explain this behaviour?

还有

  1. 如果可能的话,有人可以回答内存中正在发生的事情吗?
  1. If possible, could someone please answer what is happening in memory when this occurs every step of the way?

在过去的8个小时中,我一直在尝试使用自己的直觉(最终我陷入了JavaScript其他特性的另一个维度,但这本身就是另一个问题),但是我想知道是否有一个简单的方法或对此行为的具体解释或答案.

I have been trying to use my intuition for the last 8 hours (where I ended up spiraling into another dimension of the other peculiarities of JavaScript, but that’s another issue in itself) but I would to know if there is a simple or concrete explanation or answer to this behaviour.

在理解所有这些方面的任何帮助将不胜感激.

Any help in understanding all of this would be much appreciated.

推荐答案

我的问题是,在这两种情况下,为什么函数声明都覆盖"/阴影"还是对变量声明采用优先"?有人可以解释一下这种行为吗?

My question is, in both cases, why is the function declaration ‘overwriting’/’shadowing’ or taking ‘precedence’ over the variable declaration? Can someone please explain this behaviour?

两个片段中只有一个地方为变量a提供了一个值,这就是函数声明.语句var a;的意思是我宣布存在名为a的变量".由于函数声明负责声明它存在并为它赋值,因此var a;语句实际上没有任何作用.

There is only one place in either snippet where the variable a is given a value, and that's the function declaration. The statement var a; means "I announce that a variable named a exists". Since the function declaration is taking care of announcing that it exists and giving it a value, the var a; statement doesn't really have any effect.

实际上,如果我们参考JavaScript规范的第10.5.8节,我们可以看到,此var a;的最终结果实际上不执行任何操作,因为在处理var a;声明时已经声明了a变量.

In fact, if we refer to Section 10.5.8 of the JavaScript spec, we can see that the end result of this var a; is literally to do nothing because the a variable is already declared by the time the var a; declaration is processed.

我了解到,由于悬挂"现象,从最终结果的角度来看,这些可以说是相同的,但是为什么函数声明或变量声明在其他情况下优先于其他函数?是在创建阶段由Javascript引擎解释/解析的吗?

I understand that these arguably are the same (from the perspective of the interpreter) in terms of end-result, due to the ‘hoisting’ phenomenon, but why does the function declaration or the variable declaration have precedence over the other when it’s interpreted/parsed by the Javascript engine during the creation phase?

这里什么都没有优先".只有一个名为a的变量. var a;不会为a赋值,因此对a的值没有影响.

Nothing is taking "precedence" here. There is only one variable named a. var a; does not give a a value, so it has no effect on a's value.

也请参考第一个代码段(但这在两种情况下都适用),在第1行和第6行中声明的"a"的存储位置/地址是否完全相同?

Also, kindly refer to the first code snippet (but this applies to both cases), is the memory location/address of ‘a’ where it’s declared in line 1, and line 6, EXACTLY the same?

只有一个a变量,并且仅采用一个值,因此,这个问题通常是荒谬的,但是从广义上讲,a变量本身仅存在于一个位置,并且仅引用一个值(内存位置),即该函数的定义.

There is only one a variable and it only takes on one value, so this question is mostly nonsensical, but in rough terms, the a variable itself would only exist in one place and it would only refer to one value (memory location), which would be the definition of that function.

由于函数声明被悬挂"在变量声明之上,是否意味着在执行上下文的创建阶段结束时,第1行的"a"的内存地址指向函数对象"a"? /p>

Since function declarations are 'hoisted' above the variable declaration, does that mean that by line 1, 'a's memory address is pointing to the function object 'a', by the end of the creation phase of the execution context?

我对创建阶段的了解不是很好,但是在执行此代码的过程中,a只引用了一个值,如上所述.

My knowledge of the creation phase isn't that great, but in the course of this code's execution, a only ever refers to one value, as I said above.

在两个代码段中,"var a"都是未定义的,那么为什么在到达执行阶段(其执行点从第6行开始)时,"a"的值不是undefined?

In both code snippets, ‘var a’ is undefined, so why isn’t ‘a’ the value of undefined by the time we reach the execution phase (which has its execution point starting on line 6?)

请参阅我的第一个和第二个答案.

See my first and second answers.

为什么函数声明被覆盖或优先于变量声明是什么原因?还有

What’s the reason behind why the function declaration is overwriting or having precedence over the variable declaration? And

我已经说过,它不是采用优先级",而是JavaScript具有提升功能的原因是为了允许人们在函数实际定义之前从代码中的一行调用函数,这与其他主要语言的方式类似像C#和Java一样.

As I've said, it's not taking "precedence", but the reason JavaScript has function hoisting is in order to allow people to call functions from a line in the code before the function's actual definition, similar to how other major languages like C# and Java do.

如果可能的话,有人可以回答发生在每个步骤中的事情吗?

If possible, could someone please answer what is happening in memory when this occurs every step of the way?

JavaScript规范未定义内存中发生的情况.它定义了代码的外部行为.因此,这将是一个实现细节,并且将是特定于引擎的.

The JavaScript spec doesn't define what happens in memory. It defines the code's external behavior. So this would be an implementation detail and would be engine-specific.

在执行代码时(这两种情况下),这是一个沉重的打击:

Here is a rough blow by blow of what happens when your code is executed (in both cases):

  1. 创建一个名为a的变量,并为其分配您在其中具有的功能的值.
  2. 检查是否有一个名为a的变量.已经有一个了,所以什么也不做.
  3. 执行console.log并向其传递a的值(即该函数)
  1. Create a variable called a and assign it the value of that function you have there.
  2. Check whether there is a variable called a. There already is one, so do nothing.
  3. Execute console.log and pass it the value of a (which is that function)

这篇关于函数声明优先/覆盖变量声明?吊装?为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆