Message from JavaScript discussions
June 2017
— That is like a factory-constructor hybrid
Btw, IMO when it comes to making properties private, symbols will never be more useful than regular scope-restriction. Pipe interfaces of friend objects through a private event bus, problem solved. Instead of making every private friend-accessable member into some complicated system, you just make a single system private which enables that interface
— Yeah, I don't even use these structures that much
— I just prefer passing and returning functions rather than objects
— Probably not the best example, but just as easy as objects:
const header =
await getResponseHeaders(url);
header('location');
— Headers work better as objects, but heh
— I usually combine it with the revealing module pattern in several scope layers, in which the innermost layer can access a single object containing all members, then the next highest scope gets a different object with references to only the public members.
Like this, in a factory:
var self = {};
self.private = {
hello: "world"
};
self.public = {
what: "is up?"
};
var combined = Object.assign({}, self.public, self.private);
In a large framework I then return
combined
to the next highest scope which may contain the friend classes or the event bus to them, then return public
to a userspace scope (the original caller).
— So in a class instantistion there may be multiple objects flying around in the background, while the user only gets the public one. This usually requires special thunks instead of letting the user instantiate the classes directly
— Interesting
— Complicated, but interesting
— Heh, I always try to keep my functions tiny
— Like this:
function newThing () {
var thing = new Thing();
sendFriends(thing.combined);
return thing.public;
}