如何在一行/命令中声明具有属性的javascript函数?

如何在一行/命令中声明具有属性的javascript函数?,javascript,function,Javascript,Function,目前,我可以做到: const f = () => 'result'; f.customProperty = 5; 是否可以在一个命令中执行此操作? 比如: const f = { () => 'result', customProperty: 5, }; 好的,有一种可能性: f = (() => { const f = () => 'result'; f.customProperty = 5; return f; })(); 但是。。。还有更

目前,我可以做到:

const f = () => 'result';
f.customProperty = 5;
是否可以在一个命令中执行此操作? 比如:

const f = {
  () => 'result',
  customProperty: 5,
};
好的,有一种可能性:

f = (() => {
  const f = () => 'result';
  f.customProperty = 5;
  return f;
})();

但是。。。还有更优雅的吗?

使用
对象。赋值
,可以将函数表达式作为第一个参数传递,然后将
自定义属性
属性作为第二个参数传递对象:

const f=Object.assign(()=>result',{customProperty:5});
console.log(f.customProperty);

console.log(f())对象进行编码>。分配
,可以将函数表达式作为第一个参数传递,然后将
自定义属性
属性作为第二个参数传递对象:

const f=Object.assign(()=>result',{customProperty:5});
console.log(f.customProperty);
console.log(f())