A small tool to view real-world ActivityPub objects as JSON! Enter a URL
or username from Mastodon or a similar service below, and we'll send a
request with
the right
Accept
header
to the server to view the underlying object.
{
"@context": [
"https://join-lemmy.org/context.json",
"https://www.w3.org/ns/activitystreams"
],
"type": "Page",
"id": "https://lemmy.sdf.org/post/25214100",
"attributedTo": "https://lemmy.sdf.org/u/sebastiancarlos",
"to": [
"https://lemmy.ml/c/programmerhumor",
"https://www.w3.org/ns/activitystreams#Public"
],
"name": "Real Programmers Implement Functions On Top Of Classes",
"cc": [],
"content": "<pre style=\"background-color:#ffffff;\">\n<span style=\"font-weight:bold;color:#a71d5d;\">class </span><span style=\"color:#0086b3;\">BaseFunction </span><span style=\"color:#323232;\">{\n</span><span style=\"color:#323232;\"> </span><span style=\"font-weight:bold;color:#a71d5d;\">static </span><span style=\"color:#323232;\">#allowInstantiation = false;\n</span><span style=\"color:#323232;\">\n</span><span style=\"color:#323232;\"> </span><span style=\"font-weight:bold;color:#795da3;\">constructor</span><span style=\"color:#323232;\">(</span><span style=\"font-weight:bold;color:#a71d5d;\">...</span><span style=\"color:#323232;\">args) {\n</span><span style=\"color:#323232;\"> </span><span style=\"font-weight:bold;color:#a71d5d;\">if </span><span style=\"color:#323232;\">(</span><span style=\"font-weight:bold;color:#a71d5d;\">!</span><span style=\"color:#0086b3;\">BaseFunction</span><span style=\"color:#323232;\">.#allowInstantiation) {\n</span><span style=\"color:#323232;\"> </span><span style=\"font-weight:bold;color:#a71d5d;\">throw new </span><span style=\"color:#0086b3;\">Error</span><span style=\"color:#323232;\">(\n</span><span style=\"color:#323232;\"> </span><span style=\"color:#183691;\">"Why are you trying to use 'new'? Classes are so 2015! Use our fancy 'run' method instead!"\n</span><span style=\"color:#323232;\"> );\n</span><span style=\"color:#323232;\"> }\n</span><span style=\"color:#323232;\"> </span><span style=\"font-weight:bold;color:#a71d5d;\">for </span><span style=\"color:#323232;\">(</span><span style=\"font-weight:bold;color:#a71d5d;\">const </span><span style=\"color:#323232;\">[name, validator] </span><span style=\"font-weight:bold;color:#a71d5d;\">of </span><span style=\"color:#ed6a43;\">this</span><span style=\"color:#323232;\">.parameters()) {\n</span><span style=\"color:#323232;\"> </span><span style=\"color:#ed6a43;\">this</span><span style=\"color:#323232;\">[name] </span><span style=\"font-weight:bold;color:#a71d5d;\">= </span><span style=\"color:#323232;\">validator(args.</span><span style=\"color:#0086b3;\">shift</span><span style=\"color:#323232;\">());\n</span><span style=\"color:#323232;\"> }\n</span><span style=\"color:#323232;\"> }\n</span><span style=\"color:#323232;\">\n</span><span style=\"color:#323232;\"> </span><span style=\"font-weight:bold;color:#795da3;\">parameters</span><span style=\"color:#323232;\">() {\n</span><span style=\"color:#323232;\"> </span><span style=\"font-weight:bold;color:#a71d5d;\">return </span><span style=\"color:#323232;\">[];\n</span><span style=\"color:#323232;\"> }\n</span><span style=\"color:#323232;\">\n</span><span style=\"color:#323232;\"> </span><span style=\"font-weight:bold;color:#795da3;\">body</span><span style=\"color:#323232;\">() {\n</span><span style=\"color:#323232;\"> </span><span style=\"font-weight:bold;color:#a71d5d;\">return </span><span style=\"color:#0086b3;\">undefined</span><span style=\"color:#323232;\">;\n</span><span style=\"color:#323232;\"> }\n</span><span style=\"color:#323232;\">\n</span><span style=\"color:#323232;\"> </span><span style=\"font-weight:bold;color:#a71d5d;\">static </span><span style=\"font-weight:bold;color:#795da3;\">run</span><span style=\"color:#323232;\">(</span><span style=\"font-weight:bold;color:#a71d5d;\">...</span><span style=\"color:#323232;\">args) {\n</span><span style=\"color:#323232;\"> </span><span style=\"color:#0086b3;\">BaseFunction</span><span style=\"color:#323232;\">.#allowInstantiation </span><span style=\"font-weight:bold;color:#a71d5d;\">= </span><span style=\"color:#0086b3;\">true</span><span style=\"color:#323232;\">;\n</span><span style=\"color:#323232;\"> </span><span style=\"font-weight:bold;color:#a71d5d;\">const </span><span style=\"color:#323232;\">instance </span><span style=\"font-weight:bold;color:#a71d5d;\">= new </span><span style=\"color:#ed6a43;\">this</span><span style=\"color:#323232;\">(</span><span style=\"font-weight:bold;color:#a71d5d;\">...</span><span style=\"color:#323232;\">args);\n</span><span style=\"color:#323232;\"> </span><span style=\"color:#0086b3;\">BaseFunction</span><span style=\"color:#323232;\">.#allowInstantiation </span><span style=\"font-weight:bold;color:#a71d5d;\">= </span><span style=\"color:#0086b3;\">false</span><span style=\"color:#323232;\">;\n</span><span style=\"color:#323232;\"> </span><span style=\"font-weight:bold;color:#a71d5d;\">return </span><span style=\"color:#323232;\">instance.body();\n</span><span style=\"color:#323232;\"> }\n</span><span style=\"color:#323232;\">}\n</span><span style=\"color:#323232;\">\n</span><span style=\"font-weight:bold;color:#a71d5d;\">class </span><span style=\"color:#0086b3;\">Add </span><span style=\"font-weight:bold;color:#a71d5d;\">extends </span><span style=\"color:#0086b3;\">BaseFunction </span><span style=\"color:#323232;\">{\n</span><span style=\"color:#323232;\"> </span><span style=\"font-weight:bold;color:#795da3;\">parameters</span><span style=\"color:#323232;\">() {\n</span><span style=\"color:#323232;\"> </span><span style=\"font-weight:bold;color:#a71d5d;\">return </span><span style=\"color:#323232;\">[\n</span><span style=\"color:#323232;\"> [</span><span style=\"color:#183691;\">"a"</span><span style=\"color:#323232;\">, (x) </span><span style=\"font-weight:bold;color:#a71d5d;\">=> </span><span style=\"color:#323232;\">Number(x)],\n</span><span style=\"color:#323232;\"> [</span><span style=\"color:#183691;\">"b"</span><span style=\"color:#323232;\">, (x) </span><span style=\"font-weight:bold;color:#a71d5d;\">=> </span><span style=\"color:#323232;\">Number(x)],\n</span><span style=\"color:#323232;\"> ];\n</span><span style=\"color:#323232;\"> }\n</span><span style=\"color:#323232;\">\n</span><span style=\"color:#323232;\"> </span><span style=\"font-weight:bold;color:#795da3;\">body</span><span style=\"color:#323232;\">() {\n</span><span style=\"color:#323232;\"> </span><span style=\"font-weight:bold;color:#a71d5d;\">return </span><span style=\"color:#ed6a43;\">this</span><span style=\"color:#323232;\">.a </span><span style=\"font-weight:bold;color:#a71d5d;\">+ </span><span style=\"color:#ed6a43;\">this</span><span style=\"color:#323232;\">.b;\n</span><span style=\"color:#323232;\"> }\n</span><span style=\"color:#323232;\">}\n</span><span style=\"color:#323232;\">\n</span><span style=\"color:#795da3;\">console</span><span style=\"color:#323232;\">.</span><span style=\"color:#0086b3;\">log</span><span style=\"color:#323232;\">(Add.run(</span><span style=\"color:#0086b3;\">5</span><span style=\"color:#323232;\">, </span><span style=\"color:#0086b3;\">3</span><span style=\"color:#323232;\">)); </span><span style=\"font-style:italic;color:#969896;\">// 8\n</span><span style=\"color:#323232;\">\n</span><span style=\"color:#323232;\">\n</span><span style=\"color:#323232;\">\n</span></pre>\n",
"mediaType": "text/html",
"source": {
"content": "```js\nclass BaseFunction {\n static #allowInstantiation = false;\n\n constructor(...args) {\n if (!BaseFunction.#allowInstantiation) {\n throw new Error(\n \"Why are you trying to use 'new'? Classes are so 2015! Use our fancy 'run' method instead!\"\n );\n }\n for (const [name, validator] of this.parameters()) {\n this[name] = validator(args.shift());\n }\n }\n\n parameters() {\n return [];\n }\n\n body() {\n return undefined;\n }\n\n static run(...args) {\n BaseFunction.#allowInstantiation = true;\n const instance = new this(...args);\n BaseFunction.#allowInstantiation = false;\n return instance.body();\n }\n}\n\nclass Add extends BaseFunction {\n parameters() {\n return [\n [\"a\", (x) => Number(x)],\n [\"b\", (x) => Number(x)],\n ];\n }\n\n body() {\n return this.a + this.b;\n }\n}\n\nconsole.log(Add.run(5, 3)); // 8\n\n\n\n```",
"mediaType": "text/markdown"
},
"attachment": [
{
"href": "https://lemmy.sdf.org/pictrs/image/d227ffb4-6e8c-4033-9776-a505b3b3caca.png",
"type": "Link"
}
],
"image": {
"type": "Image",
"url": "https://lemmy.sdf.org/pictrs/image/cfdff4d5-2cf7-49e9-9f63-fe08bb0ab6c1.png"
},
"commentsEnabled": true,
"sensitive": false,
"published": "2024-11-17T00:56:15.799657Z",
"language": {
"identifier": "en",
"name": "English"
},
"audience": "https://lemmy.ml/c/programmerhumor"
}