Shorthand Function declaration

# biscotte a la crevette (11 years ago)

I would like to suggest an alternative function declaration syntax rule : pastebin.mozilla.org/4258633

That will lead to a way shorter function declaration (like Array [...] and Object {...} does ) :

long story short : <...> will be equivalent to function(...)

Which mean :

var print = <name,age>{ //eq. to var print = function(name,age){...
  console.log("my name is " + name + " " + age + "y old.\n");
}

Human = <name,age=0>{
    this.age=age;
    this.name=age;
    this.toString=<>{
        return ...
    };
}

And so, will work with anonymous function declaration (which was my main goal).

my_list.map(<val>{return val<0?-val:val;});//return absolut-ified array content

I may could be even shorter if expression-only function result were implicitly returned and it statements treated like "while" and "if" are (optional braces on single statement) :

var list=[{name:"Johnny",age:36},{name:"Mary",age:35}];
list.map(<e>e.name;);
//equivalent to :
list.map(function(e){return e.name;});

I've implemented and tested this new rule.

  • No conflict with any current rules.
  • Backward compliant (previous "old-long" declaration still work)
  • JS scene could be pleased

Do you think it could be submitted to the next ES draft ?

# Tab Atkins Jr. (11 years ago)
my_list.map(<val>{return val<0?-val:val;});//return absolut-ified array content

Are you aware of the existing arrow functions? Using current ES6, your example would be written as:

my_list.map(val=>val<0?-val:val);
# Till Schneidereit (11 years ago)

Also, there are short method declarations:

var obj = {
  foo(bar, baz) {
    // stuffs
  }
}
# Jeremy Martin (11 years ago)

Seems like this would lead to confusing operator precedence rules as well. For example:

1<<foo>{}

... is perfectly valid JavaScript today (syntactically, anyway...), and is equivalent to:

(1 << foo) > {}

... but I would assume that with the new syntax in place it would really be interpreted as:

1 < function(foo) {}

... which is also valid JavaScript. Not that either of those examples should ever occur in the real world, but in general I think they indicate that < and > might be better left alone.

# Jeremy Martin (11 years ago)

Forgot to mention, expression-only function bodies would be problematic as well.

1<foo>bar;

Is that a syntax error?

1 function(foo) { return bar; }

...or does it get interpreted as it would today?