x # Error: object 'x' not found (function() { x <<- 1 })() x # [1] 1 (function() { if (TRUE) { y <- 1 }; print(y) })() # [1] 1 (function() { if (FALSE) { y <- 1 }; print(y) })() # Error in print(y) : object 'y' not found (function() { print(y); y <- 1 })() # Error in print(y) : object 'y' not found
能夠這很 JavaScript (除了最後兩條。)code
Update:ip
R:it
(function () { a <<- 1; a <- 2 })() # variable definitions are *not* hoisted in R. a # 1
JS:io
(function () { a = 1; var a = 2 /* `var` is hoisted, but not the initial assignment */; })() a // Error: 'a' is undefined