What does it mean to distribute a function. Are regular functions iterable in javascript

I am doing something like the code below in the console

function add(a,b){return a+b;};
const obj = {...add};

To my surprise, this does not cause an error. And do not

const obj = {...123};

Propagation should only be applicable to iterable objects such as objects, arrays, string, map, set, etc. So why does he not give up and make mistakes when using indestructible objects? Or am I missing something here?

+4
source share
2 answers

Functions in JavaScript as well objects. It has its own properties, such as name, call, applyand others.

const obj = {...add} function object, obj.

function add(a,b){return a+b;};
add.test = 1;

const obj = {...add};

console.log('test' in obj);
Hide result

123 Number, -

const obj = {...123};

console.log('valueOf' in obj);
Hide result
+7

... ( ).

Rest/Spread Properties ECMAScript ( 4) . .

function add(a, b) { return a + b; }

const obj = { ...add };

console.log(obj); // no properties because no own enumerable properties
Hide result
+3

Source: https://habr.com/ru/post/1695229/


All Articles