a functional programing architecture on JavaScript/DOM
xah rumination extempore, episode №20131208194112

am gonna type as fast as i can here…

so, today i've been reworking on some of my tutorials on JavaScript/DOM scripting. For example:

JavaScript: Most Frequently Used DOM Methods
http://xahlee.info/js/dom_common_dom_methods.html

and for example, you can use document.getElementById(id) to get a element, then you can use methods such as insertBefor(), and also properties such as firstChild, nextSibling, … etc to work on it.

i always believed that computer languages or most tech don't need object oriented programing. Functional programing should be sufficient. ( for example, see What are OOP's Jargons & Complexities (OOP as Functional Programing) http://xahlee.info/comp/oop.html )

So, it occured to me, how'd i do this if it is FP?

that is, DOM is a OOP system. You have objects, and it have methods. It's a big OOP. What would be a FP system of DOM be like? So, i went on to think about that, and in 20 min roughly sketched out.

so, to do the same, as if DOM is a FP architecture, it's like this.

you use a function named
getElementById() to get a element.
this function will take 2 args, first is a data of a particular type, let's call it “document”, which represent a parsed html source code. The second arg would be the usual id.

So, a example call would be like this:
getElementById(document, "x2938")
to get a element.
the “document” there is a global, predefined variable. It is set when the FP DOM system finished parsing the html. (not much different as in OOP DOM)

then, how'd one call the method nextSibling if there's no method?

it's like this. call the function nextSibling(). It'll take one argument, a data of type node.

… so, i think the above is pretty much it. A FP architectured DOM. No OOP “objects” or “methods”. It's just all functions, and data types. The different data types corresponds to different “objects” in dom. And, we have a set of functions. Each, takes parameters, and the parameter much match type, as usual.

just a quick thought. Did i miss anything that'd be problematic for a FP DOM?

addendum: extended, on correspondence to OOP's notion of hierachy, data/methods grouping, reference vs serialization, at
http://xahlee.info/comp/object_oriented_programing_in_functional_programing_system.html
Shared publiclyView activity