函数调用javascript

Cod*_*fee 1 javascript alert function

我打电话给helloworld并用以下两种不同的方式定义它:

1)随变量

2)使用函数名称itseld

var helloWorld = function() {
    return '2';
}

function helloWorld() {
    return '1';
}

alert (helloWorld());  // This alert 2, but in absence of "var helloWorld = ....", it alert "1".
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释为什么它调用var helloWord =?而不是函数helloWorld()?

谢谢 !!

Raj*_*amy 11

为什么它调用var helloWord =?而不是函数helloWorld()?

因为functions定义将是hoisted最重要的.作业仍在同一个地方.所以它越来越好了overridden.

这是解释器看到代码的方式,

function helloWorld() {
    return '1';
}

var helloWorld;

//the above function is getting overridden here.
helloWorld = function() {
    return '2';
}

alert (helloWorld());
Run Code Online (Sandbox Code Playgroud)