Ecmascript6 introduced tail call optimization(TCO),I wrote the next code
'use strict';
var isStrict = (function() { return !this; })();
function add5(a,total=0){
if(a==1)return total+a;
return add5(a-1,total+a);
}
add5(100000);
and run it both in chrome(57.0.2987.133 (64-bit))
<script src="strict.js"></script>
and node(v8.1.0). they all print the result alike:
isStrict:true
/data/study/dashboards/api-demo/strict.js:4
function add5(a,total=0){
^
RangeError: Maximum call stack size exceeded
at add5 (/data/study/dashboards/api-demo/strict.js:4:14)
at add5 (/data/study/dashboards/api-demo/strict.js:6:12)
at add5 (/data/study/dashboards/api-demo/strict.js:6:12)
at add5 (/data/study/dashboards/api-demo/strict.js:6:12)
at add5 (/data/study/dashboards/api-demo/strict.js:6:12)
at add5 (/data/study/dashboards/api-demo/strict.js:6:12)
at add5 (/data/study/dashboards/api-demo/strict.js:6:12)
at add5 (/data/study/dashboards/api-demo/strict.js:6:12)
at add5 (/data/study/dashboards/api-demo/strict.js:6:12)
at add5 (/data/study/dashboards/api-demo/strict.js:6:12)
It seems the strict mode is enabled,but tail call optimization does not work,anybody can do me a favor and tell why?
似乎v8.1.0支持TCO,根据http://node.green/节点版本v7.10.x,v7.5.x,v6.11.x可能有效,并且可以在v7.10.0下找到 - - 我试过的和谐旗帜。
It only works in node with --harmony_tailcalls
even on version 8.1.0
.
function addFive(a, total){
'use strict';
if(a < 1) return total;
return addFive(a-1, total+a);
}
addFive(100000, 0);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.