新手求助 关于 Javascript var hoisting 和 function 的问题! - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
captainXxX

新手求助 关于 Javascript var hoisting 和 function 的问题!

  •  
  •   captainXxX Nov 2, 2016 2466 views
    This topic created in 3471 days ago, the information mentioned may be changed or developed.

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

    4 replies    2016-11-05 17:59:22 +08:00
    fds
        1
    fds  
       Nov 2, 2016
    declaration 是指 声明,不是定义。声明会提升到前面。也就是说你的代码相当于:

    var test
    Onclick= test // 此时值为 undefined
    test = function(){}
    fds
        2
    fds  
       Nov 2, 2016
    btn.Onclick=anonymousFunctionName{} 这根本就不符合语法吧?{}是什么?
    你要是定义了匿名函数,比如
    var anOnymousFunctionName= function(){}
    btn.Onclick= anonymousFunctionName
    这肯定是可以的。那句英文是说如果你
    var anOnymousFunctionName= function(){}
    btn.Onclick= anonymousFunctionName()
    就不对了,因为你是调用了函数获得了返回值,而不是把函数本身传给了 onclick 。
    otakustay
        3
    otakustay  
       Nov 2, 2016
    变量的声明会上提,并不是变量的赋值会上提,这是 2 个过程

    所以当你执行 btn.Onclick= test 的时候, test 处于“已经声明,未赋值”的状态,即 undefined
    MRwaite
        4
    MRwaite  
       Nov 5, 2016
    关键词:函数声明 && 函数表达式 && 匿名函数
    About     Help     Advertise     Blog     API     FAQ     Solana     2909 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 35ms UTC 06:34 PVG 14:34 LAX 23:34 JFK 02:34
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86