新手求助 javascript 问题!

2016-11-02 12:37:25 +08:00
 captainXxX

'''javascript btn.onclick = test;

  var test = function() {
    // displayMessage('Your inbox is almost full — delete some mails', 'warning');
    displayMessage('Brian: Hi there, how are you today?','chat');
    displayMessage('Brian: Hi there, how are you today?');
  }
  
   function displayMessage(msgText, msgType){
      ......
  }

'''

Because vareiable declarations(and declarations in general) are processed before any code is executed, declaring a variable anywhere in the code is equivalent to declaring it at the top. 这是 MDN 上面的原话。。但是把 btn.onclick = test;挪到 test 变量定义的下面就正确了。这是为什么??? 还有一点: btn.onclick=functionName;这是对的 但是 btn.onclick=anonymousFunctionName{}就是错的。必须要加上括号。。 但是 The parentheses in this context are sometimes called the "function invocation operator" 这也是 MDN 上面的原话。这又是为什么

2245 次点击
所在节点    JavaScript
12 条回复
morethansean
2016-11-02 12:48:12 +08:00
……楼主我觉得你学习的方法不太对啊……

第一个问题,变量声明 hoisted ,赋值又没有,下面这段代码 console.log(a); var a = 123; console.log(a); 等价于 var a; console.log(a); a = 123; console.log(a);

然后你说的第二个问题……我有点不知道你想说什么……但明显这两个问题都是因为你的理解有问题……特别是你只帖了英文原文( which 字面上并没有错),而并没有用中文说出你自己的理解(通常如果这里你打的是中文,也就是说是你自己翻译的版本那么就很容易看出问题了,而你只是 copy 了原文问为什么,直接反应了你的理解是有问题的……)。
fengxiang
2016-11-02 13:00:25 +08:00
第二个问题原文是这个?

You might be wondering why we haven't included the parentheses after the function name. This is because we don't want to call the function immediately — only after the button has been clicked. If you try changing the line to and saving and reloading, you'll see that the message box appears without the button being clicked! The parentheses in this context are sometimes called the "function invocation operator". You only use them when you want to run the function immediately in the current scope. In the same respect, the code inside the anonymous function is not run immediately, as it is inside the function scope.
meszyouh
2016-11-02 13:04:13 +08:00
兄弟你有点凶残啊。
第一个问题,函数表达式解析到那里才可以调用。第二个,你说的好像是 function 后面加括号才可以?。。。
ryanzyy
2016-11-02 13:08:52 +08:00
```
javascript btn.onclick = test;

function test() {
// displayMessage('Your inbox is almost full — delete some mails', 'warning');
displayMessage('Brian: Hi there, how are you today?','chat');
displayMessage('Brian: Hi there, how are you today?');
}

function displayMessage(msgText, msgType){
......
}
```
exoticknight
2016-11-02 13:08:56 +08:00
不推荐用 var 的方式来定义 function
直接用 function foo () {},这样可以避免变量提升的问题
bramblex
2016-11-02 13:14:52 +08:00
@exoticknight 不建议用 function xxx 来定义。

建议用 const xxx = function ... 来定义
Nutlee
2016-11-02 13:18:11 +08:00
第一个问题你看一下 js 的 “声明提升” 吧。
关于第二个问题,请注意 js 中使用 function 声明函数变量中只有
```
function foo() {}
```

和匿名函数对象

```
function() {}
```

这两种形式, btn.onclick 后面要等于的是函数对象的引用,所以,你所引用的函数对象要合法....

似乎,这是很基础的问题..
crazyfrog
2016-11-02 13:47:01 +08:00
不讲规矩,当然不能过...
captainXxX
2016-11-02 13:50:49 +08:00
@Nutlee 函数对象的引用是函数名, 项要获得匿名函数对象的引用要完整定义??
captainXxX
2016-11-02 13:51:25 +08:00
ukauka
2016-11-02 22:54:56 +08:00
test 在最开始声明了,但没有赋值
你写的代码相当于下面这样
var test;
btn.onclick = test; //undefined

test = function() {
// displayMessage('Your inbox is almost full — delete some mails', 'warning');
displayMessage('Brian: Hi there, how are you today?','chat');
displayMessage('Brian: Hi there, how are you today?');
}

function displayMessage(msgText, msgType){
......
}
Nutlee
2016-11-16 09:29:36 +08:00
@captainXxX 你可以这里理解,一个正确的 function 声明,不管是不是匿名的返回值都是这个 function 的引用,所以,在需要写 function 引用的地方,你就知道怎么写了吧。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/317376

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX