Published
Edited
Nov 18, 2020
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
// Constructor functions are not called with new
// They take some form of specification or config object as an argument
// although could take any set of arguments as they are just normal functions
function constructor(spec) {
// Declare variables scoped to this function
// All these are inside the function's scope and are inaccessibe from outside
let
// Destructure and process items coming from the spec
// potentially assinging defaults and/or error checking as required
{member} = spec,
// Call other constructors and extra data/behaviors
{other} = otherConstructor(spec),
// Build methods that close over everything available in its containing block
method = function () {
// member, other, method, spec, etc. are available here, e.g.
return member + other;
};
// Anything that forms part of the public interface is put in the outgoing object
// This object is then frozen, creating a hard, uncoruptible interface
return Object.freeze({
method,
other,
});
}
Insert cell
// This is another constructor to demonstrate calling constructors in the above
function otherConstructor(spec) {
return Object.freeze({
other: 42,
});
}
Insert cell
Insert cell
obj = constructor({ member: 1 });
Insert cell
Insert cell
obj.other;
Insert cell
obj.other = 0;
Insert cell
Insert cell
obj.method();
Insert cell
Insert cell
Insert cell
function chainWith(returnedObj, fluentMethod) {
return function (...args) {
fluentMethod(...args);
return returnedObj;
};
}
Insert cell
function conditionalChainWith(returnedObj, interceptObj, fluentMethod) {
return function (...args) {
const result = fluentMethod(...args);
return result === interceptObj ? returnedObj : result;
};
}
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more