What Is An Idiomatic Way To Put Fields/attributes On A Function In Modern Javascript (es5 Or Later)?
I would like to do something like this: 'use strict'; // on by default in my babel setup function create_func(some, params) { var func = otherstuff => { console.log(
Solution 1:
If your function is supposed to be a class with static properties, use class
syntax.
Otherwise, you can use Object.assign
:
function create_func(some, params) {
return Object.assign(otherstuff => {
console.log("inner", some, otherstuff);
return 4;
}, {params});
}
Of course simple assignment continues to work - there's no new syntax to create properties on function objects specifically.
Can you create functions with custom prototypes in JavaScript?
If that is what you are after (not just giving the function object custom properties), you can subclass Function
since ES6 but it's not exactly convenient.
Post a Comment for "What Is An Idiomatic Way To Put Fields/attributes On A Function In Modern Javascript (es5 Or Later)?"