Javascript中的模糊函数声明 [英] Ambiguous function declaration in Javascript

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

问题描述

我是Javascript的新手,对函数声明的工作方式感到困惑。我做了一些测试,并得到了一些有趣的结果:

  say(); 

函数say()
{
alert(say);
}

前向声明工作并弹出say



相反

  say(); 

say = function()
{
alert(say);
}

不起作用,尽管它还声明了一个函数对象



如果我们声明函数并在之后重新声明:

  function say()
{
alert(speak);
}

say();

函数say()
{
alert(say);
}

我得到了说而不是说。这是惊喜!



好的。似乎只有最新的函数声明才起作用。然后让我们先声明函数对象,然后再声明一个常规函数:

  say = function()
{
alert(发言);
}

say();

函数say()
{
alert(say);
}

say();

另一个惊喜是,它是发言,然后是发言。 常规函数声明完全不起作用!



是否有解释所有这些声明?而且,如果常规函数声明真的是脆弱的,并且可以很容易地被具有相同名称的函数对象覆盖,那我应该远离吗?



另一个问题是:只有函数对象格式,这个前向声明变得不可能了吗?有什么方法可以在Javascript中模拟?

解决方案

Javascript的工作原理如下:

解析文档,并在执行实际语句之前立即考虑函数声明。这解释了你的第一个例子。



如果你将一个函数赋值给一个局部变量,这个变量是在执行过程中完成的,所以你不能在你的第二个例子中使用该方法。



你所经历的是,如果你声明了一个函数两次,最后一个函数将被整个应用程序使用。这是你的第三个例子。

这些函数是窗口对象的成员,它们实际上是全局声明的。如果将局部变量赋值给函数的值,则该局部变量优先于窗口对象中的成员。如果javascript找不到本地变量,它会在范围内搜索以找到它,窗口对象是最后的手段。这就是你最后一个例子发生的事情,它有一个变量 say ,它比全局函数 say 。



如果您在运行时重新声明 say ,即交换上例中声明的顺序,则你会看到你期望的两种不同的警报:

  say(); //说出全局函数

函数say(){
alert('speak');
}

var say = function(){
alert('say');
}

say(); //说声明的局部变量


I am new to Javascript and got confused by how the function declaration works. I made some test on that and got some interesting results:

say();

function say()
{
    alert("say");
}

The forward-declaration worked and popup "say"

On the opposite

say();

say = function()
{
    alert("say");
}

did not work, although it also declared a function object

If we declare the function and redeclare that afterwards:

function say()
{
    alert("speak");
}

say();

function say()
{
    alert("say");
}

I got "say" instead of "speak". That's surprise!

OK. It seems that only the latest function declaration works. Then lets declare function object first and then a "regular" function:

say = function()
{
    alert("speak");
}

say();

function say()
{
    alert("say");
}

say();

Another surprise, it was "speak" followed by "speak". The "regular" function declaration did not work at all!

Is there an explanation of all of them? And, if the "regular" function declaration is really that "fragile" and can be easily override by the function object with same name, should I stay away from that?

Another question is: with only the function object format, does that forward-declaration become impossible? Is there any way to "simulate" that in Javascript?

解决方案

Javascript works like this:

The document is parsed, and the function declarations are all taken taken into account immediately, before the execution of the actual statements occur. This explains your first example.

If you assign a function to a local variable, that is done during the execution, so you can't use the method in your second example.

What you experience is that if you declare a function twice, the last one will be used by the entire application. That's your third example.

These functions are made members of the window object, they are in effect declared globally. If you assign a local variable to a value of a function, then that local variable takes precedence over members in the window object. If javascript can't find a local variable, it searches up in scope to find it, the window object being the last resort. That's what happened in your last example, it has a variable say that is in a more specific scope than the global function say.

If you would redeclare say at runtime, i.e. swap the order of declarations in your last example, then you would see the two different alerts you'd expect:

say(); //speak, the global function

function say() {
  alert('speak');
}

var say = function() {
  alert('say');
}

say(); //say, the declared local variable

这篇关于Javascript中的模糊函数声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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