njs
njs copied to clipboard
Support for ES6 optional function parameter
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters
just want to note, that we need an additional scope, if default parameters are present:
.editor
// Entering editor mode (^D to finish, ^C to cancel)
var x = function(a = 1, b = 1, c = () => a + b) {
var a = 2, b = 2;
return c();
};
var y = function(a, b, c) {
a = 1;
b = 1;
c = () => a + b;
var a = 2, b = 2;
return c();
};
console.log(x(), y());
2 4
undefined
Hello, I would like to get this feature too.
Is there some kind of ETA when the feature gets available?
@sly-roar
The feature is in progress, is planned for 0.7.0.
Known bugs to fix:
>> var x = function(a = 1, b = 1, c = () => a + b) {
var a = 2, b = 2;
return c();
};
>> x();
4
// FAIL
>> function f(a=()=>b, b=1) {return a}; f()()
Thrown:
ReferenceError: cannot access variable before initialization
at anonymous (t2.js:1)
at console.log (native)
at main (t2.js:3)
// FAIL
>> ((x = 42) => {}).length
1
// FAIL
>> ((a,b=1) => {}).length
2
// FAIL
>> var f = function(x = arguments[2], y = arguments[3], z) { return y; };
>> f(undefined, undefined, 'third', 'fourth');
'third'
// FAIL
@lexborisov @xeioex
and some more tests:
>> var x = function(a = 1, b = 1, c = () => a + b) { let a = 2, b = 2; return c(); }
Thrown:
SyntaxError: "a" has already been declared in shell:1
// OK
>> var x = function(a = () => 1, b = () => 1, c = () => a() + b()) { function a() { return 2; }; return c(); }
undefined
>> x()
2
// OK
>> var x = function(a = () => 1, b = () => 1, c = () => a() + b()) { var a = () => 2; return c(); }
undefined
>> x()
3
// FAILED
>> var x = function(a = () => 1, b = () => 1, c = () => a() + b()) { function a() { return 2; } return c() + a(); }
undefined
>> x()
3
// FAILED
maybe unrelated:
$ build/njs -q
>> var x = () => (x = a) => x; let a = 2; x()();
2
// OK
>>
$ build/njs -q
>> var x = () => (x = a) => x;
undefined
>> var a = 2;
undefined
>> x()()
2
// OK
>>
$ build/njs -q
>> var x = () => (x = a) => x;
undefined
>> let a = 2;
undefined
>> x()()
Thrown:
ReferenceError: "a" is not defined
at anonymous (:1)
at main (:1)
// FAIL
>>
>> var x = async function(inner = () => Promise.reject('reject'), next = await inner()) {};
undefined
// vs.
> var x = async function(inner = () => Promise.reject('reject'), next = await inner()) {};
var x = async function(inner = () => Promise.reject('reject'), next = await inner()) {};
^^^^^
Uncaught SyntaxError: Illegal await-expression in formal parameters of async function
@drsm
Unfortunately, in the current architecture, we cannot solve closures problem:
function x(a = 1, b = 1, c = () => a + b) {
console.log(a, b);
let f = () => {return a + b}
var a = 2, b = 3;
console.log(f());
return c();
};
console.log(x());
It was decided to postpone this task. We will return to it after the release. Thank for testing!
Unfortunately, in the current architecture, we cannot solve closures problem
BTW, I didn't ever see that someone use that "feature" in the wild.
@drsm We decided to postpone this feature because of non-trivial limitations. In the current architecture we cannot guarantee correct execution (and catching all possible unsupported cases) when there are function values as a default arguments.
After 0.7.0 @lexborisov is going to start working on the frames refactoring, when C-like frames are removed implementing this feature correctly will be much simpler. Removing C-like frames will also require implementing garbage collector.