Open cubranic opened 10 years ago
I think aside from variable hoisting, it's pretty fair to describe the scoping as JS-like. And since hoisting is (arguably) a quirk in JS and lack of hoisting is not a quirk in R, I don't think we need a clarification. I don't think we're making a rigorous claim, just giving a gist.
Eventually ES6 implementations will start to use let
and we'll have block scoping in JS, but that's another story. :)
Hello; thanks for the feedback! I think the relatively narrow claim the text makes (that neither Javascript or R has block scope) is correct and is not likely to be confusing, but maybe I'm underestimating the importance of hoisting to Javascript developers.
While it's true that variable scope is not per-block, it's not the same as in JavaScript, where local variables are hoisted to top of the function:
Example from Mozilla.org:
Try the same in R and you'll get "Error in print(myvar) : object 'myvar' not found":
But yes, there is no block scope:
will print "local variable". For block-scope, you can use
local
:will throw: "Error in print(myvar) : object 'myvar' not found"