需要了解Javascript函数的例子 [英] Need to understand Javascript function hoisting example

查看:90
本文介绍了需要了解Javascript函数的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了Javascript Hoisting的概念,它非常令人困惑,但我看到了一些例子,并且明白了实际提升的意义。

所以基本上是JavaScript将所有声明移动到当前范围的顶部(到当前脚本或当前函数的顶部)的默认行为。

但是我无法理解下面的实现:

  var is_android = true; 
if(is_android){
function foo(){
alert('我是Android');
}
} else {
function foo(){
alert('我不是Android');
}
}
foo();

输出结果在警告框中显示我不是Android



我想知道为什么从else块调用 foo(),即使 is_android 值是真的。



任何帮助将不胜感激。

解决方案

tl; dr:不要在块中使用看起来像函数声明的东西,尤其不要使用条件。
$ b




事实是,大多数浏览器都以错误的方式解释这段代码。他们将函数定义作为声明函数处理,尽管函数声明不允许在块内使用,因为函数声明 不是声明,它是 源代码元素



这个工作原理一般:

在代码甚至执行,解释器会查找所有变量和函数声明,而不管它们在哪里,并在当前/新环境中为它们创建绑定。然后它开始实际执行代码。

因此,假设函数定义被解释为声明,由于有两个声明,所以最后一个一个胜利。你的代码基本上变成了:

  function foo(){
alert('I am Android');

函数foo(){
alert('我不是Android');
}
var is_android;

is_android = true;
if(is_android){

} else {

}
foo();

其他引擎会用不同的方式解释,但仍然不正确(IMO,见下文):

< blockquote>

在以下脚本中,零函数从未定义过,因为'if(0)'将其条件评估为false,所以无法调用:

  if(0){
function zero(){
document.writeln(This is zero。);


$ / code>

注意:某些JavaScript引擎(不包括SpiderMonkey)错误地将任何具有名称的函数表达式视为函数定义。这将导致零被定义,即使总是假如果条件。有条件地定义函数的更安全的方法是匿名定义函数并将其分配给变量:

  if(0){
var zero = function(){
document.writeln(This is zero。);


$ / code>


如果函数定义真正被解释为函数表达式(类似于(function zero(){...})),那么该函数的名称在包含范围内不可访问,并且该函数将丢失。


I read the concept of Javascript Hoisting.Its pretty confusing but I saw some examples and got the idea what hoisting actually does.

So basically "Hoisting is JavaScript's default behavior of moving all declarations to the top of the current scope (to the top of the current script or the current function)."

But I am not able to understand the below implementation :

var is_android = true;
if (is_android) {
    function foo() {
        alert('I am Android');
    }
} else {
    function foo() {
        alert('I am NOT Android');
    }
}
foo();

The output shows "I am NOT Android" in alert box.

I want to know why foo() is called from else block even if is_android value is true .

Any help will be appreciated.

解决方案

tl;dr: Don't use something that looks like a function declaration inside a block, especially not a conditional.


Fact is, most browsers interpret this piece of code the wrong way. They treat the function definitions as function declarations, even though function declarations are not allowed inside blocks, since a function declaration is not a statement, it's a source element.

This how it works in general:

Before the code is even executed, the interpreter looks for all variable and function declarations, not matter where they are, and creates a binding for them in the current/new environment. Then it starts actually executing the code.

So, assuming that the function definitions are interpreted as declarations, since there are two declarations, the last one wins. Your code basically becomes:

function foo() {
    alert('I am Android');
}
function foo() {
    alert('I am NOT Android');
}
var is_android;

is_android = true;
if (is_android) {

} else {

}
foo();

Other engines would interpret it differently, but still incorrectly (IMO, see below):

In the following script, the zero function is never defined and cannot be invoked, because 'if (0)' evaluates its condition to false:

if (0) {
   function zero() {
      document.writeln("This is zero.");
   }
}

Note: Some JavaScript engines, not including SpiderMonkey, incorrectly treat any function expression with a name as a function definition. This would lead to zero being defined, even with the always-false if condition. A safer way to define functions conditionally is to define the function anonymously and assign it to a variable:

if (0) {
   var zero = function() {
      document.writeln("This is zero.");
   }
}

But in this case, if the function definition truly was interpreted as function expression (similarly to (function zero() { ... })), then the name of the function would not be accessible in the containing scope, and the function would just be lost.

这篇关于需要了解Javascript函数的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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