The this
needs to refer to the map value object for chaining to work. This works very naturally if you simply change the action
and process
to normal functions and return this
from inside them. (Not from bar
or vaz
, since they aren't being called with any this
)
function bar() {
console.log("bar");
}
function vaz() {
console.log("vaz");
}
const configMap = new Map([
[
"Document",
{
action: function() { bar(); return this; },
process: function() { vaz(); return this; },
}
]
//...
]);
configMap.get("Document").action().process();
If you must use arrow functions and want this to work, you'll have to put the document object into a variable and return it instead of this
.
function bar() {
console.log("bar");
}
function vaz() {
console.log("vaz");
}
const documentObj = {
action: () => { bar(); return documentObj; },
process: () => { vaz(); return documentObj; },
};
const configMap = new Map([
["Document", documentObj]
//...
]);
configMap.get("Document").action().process();
Without declaring it outside, you'd need an IIFE.
function bar() {
console.log("bar");
}
function vaz() {
console.log("vaz");
}
const configMap = new Map([
[
"Document",
(() => {
const documentObj = {
action: () => { bar(); return documentObj; },
process: () => { vaz(); return documentObj; },
};
return documentObj;
})(),
]
//...
]);
configMap.get("Document").action().process();
But a plain function
would be easier, as in the first snippet.