Platform
Resources
Pricing
Sign in
Get started
Brendan Ratter
Workspace
Fork
Published
By
Brendan Ratter
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
Purpose-built for displays of data
Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Try it for free
Learn more
Fork
View
Export
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
constructor
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
otherConstructor
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
obj
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
chainWith
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
conditionalChainWith
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML