Middle
Is it possible to write your own properties in a function if functions belong to objects?
sobes.tech AI
Answer from AI
Yes, in JavaScript functions are objects, so you can add your own properties to them. This allows you to store additional information or state directly in the functions.
Example:
function counter() {
counter.count = (counter.count || 0) + 1;
console.log(counter.count);
}
counter(); // 1
counter(); // 2
counter(); // 3
Here, the count property is stored inside the counter function and increases with each call.