You can define as many functions-within-functions as you like!
let f1 = function() {
let f2 = function() {
let f3 = function() {
return 'functions';
};
return 'love ' + f3();
};
return 'I ' + f2();
};
console.log(f1());
If you want to define functions as properties, you could also do the following:
let f1 = function() {
return 'I ';
};
f1.f2 = function() {
return 'love ';
};
f1.f3 = function() {
return 'functions';
};
console.log(f1() + f1.f2() + f1.f3());
Note that this property assignment works exactly the same way that objects work; e.g.
let f1 = function(){}; f1.someProperty = 'someValue';
vs
let o = {}; o.someProperty = 'someValue';
The assignment of properties to a function doesn't need to occur within the function body, but functions certainly could assign properties to themselves:
let f = function() {
f.a = 'b';
f.c = 'd';
};
Note that in this case the f.a
and f.c
properties wouldn't exist until you actually call the function by performing: f()
.