最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can anyone else use let statements at their Node.js REPL? - Stack Overflow

programmeradmin0浏览0评论

Is it possible? It doesn't seem to work in my REPL, neither with nor without --harmony.

What I'd really like to do is use for..of loops, but let seems a simpler thing to troubleshoot and is likely the same reason.

Anyone know anything about the status of these?

Is it possible? It doesn't seem to work in my REPL, neither with nor without --harmony.

What I'd really like to do is use for..of loops, but let seems a simpler thing to troubleshoot and is likely the same reason.

Anyone know anything about the status of these?

Share Improve this question edited Nov 14, 2013 at 1:30 user2958725 asked Nov 14, 2013 at 1:23 user2958725user2958725 1,4453 gold badges13 silver badges16 bronze badges 1
  • hey can you mark the answer on this question? – jcollum Commented Aug 28, 2014 at 15:54
Add a ment  | 

2 Answers 2

Reset to default 8
$ node --version
v0.10.13

It was a bit cryptic, you'd think just --harmony would work, but you need to add in a use strict somewhere (which you can do at the mand line):

$ node --harmony --use-strict
> var letTest = function () {
...   let x = 31;
...   if (true) {
.....     let x = 71;  // different variable
.....     console.log(x);  // 71
.....   }
...   console.log(x);  // 31
... }
undefined
> letTest()
71
31
undefined
> 

Much happy!

However, I tried a simple of prehension and it didn't work:

[ square(x) for (x of [1,2,3,4,5]) ]

With no luck. It looks like you may have to go past the current stable release to get all the harmony features.

If you run it from a file, node.js will tell you the error :

SyntaxError: Illegal let declaration outside extended mode

Its details are given in another question What is extended mode? As it happens the extended mode is built on strict mode so you can't use it without "use strict" and harmony flag both. The reason I will quote from here:

Recall that ES5 defines "strict mode", a new mode of execution for JS. Let's call the other mode "classic mode". ES6 defines a third "extended mode", which builds on strict mode, and enables the new features.

The recent node v11.7 has iterators which allow you to use for of loops. Example I used :

function* fibonacci() {
    let prev = 0, curr = 1, temp;
    for (;;) {
        temp = prev;
        prev = curr;
        curr = temp + curr;
        yield curr;
    }
}

for (let n of fibonacci()) {
    if (n > 1000)
        break;
    console.log(n);
}

For now, I could only use for of over iterators and not simple arrays.

发布评论

评论列表(0)

  1. 暂无评论