V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
captainXxX
V2EX  ›  JavaScript

新手求助 javascript 问题!

  •  
  •   captainXxX · 2016-11-02 12:37:25 +08:00 · 2192 次点击
    这是一个创建于 2733 天前的主题,其中的信息可能已经有所发展或是发生改变。

    '''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 上面的原话。这又是为什么

    12 条回复    2016-11-16 09:29:36 +08:00
    morethansean
        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 了原文问为什么,直接反应了你的理解是有问题的……)。
    fengxiang
        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.
    meszyouh
        3
    meszyouh  
       2016-11-02 13:04:13 +08:00 via Android
    兄弟你有点凶残啊。
    第一个问题,函数表达式解析到那里才可以调用。第二个,你说的好像是 function 后面加括号才可以?。。。
    ryanzyy
        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){
    ......
    }
    ```
    exoticknight
        5
    exoticknight  
       2016-11-02 13:08:56 +08:00
    不推荐用 var 的方式来定义 function
    直接用 function foo () {},这样可以避免变量提升的问题
    bramblex
        6
    bramblex  
       2016-11-02 13:14:52 +08:00 via Android   ❤️ 1
    @exoticknight 不建议用 function xxx 来定义。

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

    和匿名函数对象

    ```
    function() {}
    ```

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

    似乎,这是很基础的问题..
    crazyfrog
        8
    crazyfrog  
       2016-11-02 13:47:01 +08:00
    不讲规矩,当然不能过...
    captainXxX
        9
    captainXxX  
    OP
       2016-11-02 13:50:49 +08:00
    @Nutlee 函数对象的引用是函数名, 项要获得匿名函数对象的引用要完整定义??
    captainXxX
        10
    captainXxX  
    OP
       2016-11-02 13:51:25 +08:00
    ukauka
        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){
    ......
    }
    Nutlee
        12
    Nutlee  
       2016-11-16 09:29:36 +08:00
    @captainXxX 你可以这里理解,一个正确的 function 声明,不管是不是匿名的返回值都是这个 function 的引用,所以,在需要写 function 引用的地方,你就知道怎么写了吧。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1000 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 19:18 · PVG 03:18 · LAX 12:18 · JFK 15:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.