nzakas / understandinges6

Content for the ebook "Understanding ECMAScript 6"
5.45k stars 796 forks source link

Redeclaration with let statement won't raise error #56

Closed meetwudi closed 10 years ago

meetwudi commented 10 years ago

In the Let declarations part, you mentioned that if I declared a variable with var and then redeclare it with let, it will raise error. But actually it doesn't. I ran the following code in latest v8 (harmony mode), which turned out to be just fine.

> var test = 1;
> let test = 1;

What will actually raise the error is declaring two variables with same name with let in the same scope. For example:

> let test = 1;
> let test = 2;
SyntaxError: Variable 'test' has already been declared
    at Object.exports.createScript (vm.js:44:10)
    at REPLServer.defaultEval (repl.js:118:23)
    at bound (domain.js:255:14)
    at REPLServer.runBound [as eval] (domain.js:268:12)
    at REPLServer.<anonymous> (repl.js:277:12)
    at REPLServer.EventEmitter.emit (events.js:104:17)
    at REPLServer.Interface._onLine (readline.js:202:10)
    at REPLServer.Interface._line (readline.js:531:8)
    at REPLServer.Interface._ttyWrite (readline.js:806:14)
    at ReadStream.onkeypress (readline.js:101:10)
getify commented 10 years ago

These engines are not up to spec. The condition you state is an error condition, regardless of v8 not being compliant yet.

meetwudi commented 10 years ago

Thanks!

yoshuawuyts commented 10 years ago

Wasn't let removed from the spec some time ago? Remember seeing a talk by @getify about it.

rwaldron commented 10 years ago

@yoshuawuyts let is still in the spec

getify commented 10 years ago

I know what @yoshuawuyts is probably talking about.

I mention in my "New Rules For JS" talk (as well as my "YDKJS: Scope & Closures" book) that the let (x = ..) { .. } (aka "let statement") form of let was not added to the ES6 spec, which is true. However, the let x = .. (aka "let declaration") form of let _WAS_ added to the ES6 spec.

As far as I can tell, @nzakas is only covering the standardized "let declaration" form in his book, so everything he's said is accurate.

nzakas commented 10 years ago

That is correct.​ I'm covering what should work based on the current state of the spec.