'''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 上面的原话。这又是为什么
1
morethansean 2016-11-02 12:48:12 +08:00 1
……楼主我觉得你学习的方法不太对啊……
第一个问题,变量声明 hoisted ,赋值又没有,下面这段代码 console.log(a); var a = 123; console.log(a); 等价于 var a; console.log(a); a = 123; console.log(a); 然后你说的第二个问题……我有点不知道你想说什么……但明显这两个问题都是因为你的理解有问题……特别是你只帖了英文原文( which 字面上并没有错),而并没有用中文说出你自己的理解(通常如果这里你打的是中文,也就是说是你自己翻译的版本那么就很容易看出问题了,而你只是 copy 了原文问为什么,直接反应了你的理解是有问题的……)。 |
2
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. |
3
meszyouh 2016-11-02 13:04:13 +08:00 via Android
兄弟你有点凶残啊。
第一个问题,函数表达式解析到那里才可以调用。第二个,你说的好像是 function 后面加括号才可以?。。。 |
4
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){ ...... } ``` |
5
exoticknight 2016-11-02 13:08:56 +08:00
不推荐用 var 的方式来定义 function
直接用 function foo () {},这样可以避免变量提升的问题 |
6
bramblex 2016-11-02 13:14:52 +08:00 via Android 1
|
7
Nutlee 2016-11-02 13:18:11 +08:00
第一个问题你看一下 js 的 “声明提升” 吧。
关于第二个问题,请注意 js 中使用 function 声明函数变量中只有 ``` function foo() {} ``` 和匿名函数对象 ``` function() {} ``` 这两种形式, btn.onclick 后面要等于的是函数对象的引用,所以,你所引用的函数对象要合法.... 似乎,这是很基础的问题.. |
8
crazyfrog 2016-11-02 13:47:01 +08:00
不讲规矩,当然不能过...
|
9
captainXxX OP @Nutlee 函数对象的引用是函数名, 项要获得匿名函数对象的引用要完整定义??
|
10
captainXxX OP @fengxiang 对
|
11
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){ ...... } |
12
Nutlee 2016-11-16 09:29:36 +08:00
@captainXxX 你可以这里理解,一个正确的 function 声明,不管是不是匿名的返回值都是这个 function 的引用,所以,在需要写 function 引用的地方,你就知道怎么写了吧。
|