考虑这段代码:
function foo(a) {
console.log("Mul =", a);
return a * 2;
};
function * process(start) {
// next() #1
var result = start;
console.log("Pre-processing =", result);
result = yield foo(result);
// next() #2
console.log("Process result 1 =", result);
result = yield foo(result);
// next() #3
console.log("Process result 2 =", result);
result = yield foo(result);
// next() #4
console.log("Process result 3 =", result);
return foo(result);
}
var it = process(1);
console.log("#1");
console.log("Next 1 =", /*#1*/it.next("bar"));
console.log("#2");
console.log("Next 2 =", /*#2*/it.next(3));
console.log("#3"); …Run Code Online (Sandbox Code Playgroud) 我正在写生成器的简单函数
function simpleGenerator(){
yield "first";
yield "second";
yield "third";
};
var g = simpleGenerator();
console.log(g.next());
Run Code Online (Sandbox Code Playgroud)
它是为了屈服线——
SyntaxError: missing ; before statement
Run Code Online (Sandbox Code Playgroud)
我无法得到显示错误的原因...如果我使用 return like
function simpleGenerator(x){
while(true){
var a=x*2;
return a;
}
}
var g = simpleGenerator(2);
console.log(g);
Run Code Online (Sandbox Code Playgroud)
它工作正常,
问题是:
longhao33@hePC:~$ node --harmony test.js
/home/longhao33/test.js:1
(function (exports, require, module, __filename, __dirname) { let str = 'es666666666666';
^^^
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:413:25)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:475:10)
at startup (node.js:117:18)
at node.js:951:3
Run Code Online (Sandbox Code Playgroud)
但是:"让"在以下情况下得到支持:(太奇怪了!)
longhao33@hePC:~$ node --harmony
> let str = 'es66666666666'
undefined
> str
'es66666666666'
Run Code Online (Sandbox Code Playgroud)
test.js的内容:
让str ='es666666666666'; 的console.log(STR);
提前谢谢.
ES5和ES6之间的行为差异要小得多.这些差异可能会导致在使用现代浏览器(支持ES6)进行解释时在ES5中编写的脚本出错.这个问题如何解决?
javascript cross-browser ecmascript-5 ecmascript-harmony ecmascript-6