Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Diagrams for JavaScript functions

What tools can be used to convey concepts like JavaScript variable scoping and closures clearly in something similar to UML sequence diagrams? For example, how can code like the following: (the Infamous Loop Problem)

var arr = [];
for(var i=0; i<10; i++) {
    arr.push(function() { alert(i); });
}
for(var j=arr.length;j--;) {
    arr[j]();
}

... be clearly explained in a diagram similar to this one:

A blank UML sequence diagram

like image 793
Richard JP Le Guen Avatar asked Jun 02 '11 16:06

Richard JP Le Guen


People also ask

Does JavaScript use UML?

It seems that you can use UML for JavaScript, but it will depend on how you are going to approach your work. Specifically JS/UML can automate UML documentation for JSDoc, Dojo, YUI.

What is JavaScript in diagram?

The JavaScript Diagram is a feature-rich architecture diagram library for visualizing, creating, and editing interactive diagrams. It supports creating flowcharts, organizational charts, mind maps, and BPMN charts either through code or a visual interface.

What are functions () in JavaScript?

A function in JavaScript is similar to a procedure—a set of statements that performs a task or calculates a value, but for a procedure to qualify as a function, it should take some input and return an output where there is some obvious relationship between the input and the output.

What is a class diagram in JavaScript?

Class diagram is used to represent the static view of an application. The class diagrams are widely used in the modelling of object oriented systems because they are the only UML diagrams which can be mapped directly with object-oriented languages.


3 Answers

The code is an arbitrary example. The code has nothing to do with the question, merely demonstrates often misleading code which could benefit from being described.

You can not describe closures and scoping in UML. There is simply no support for it, not in sequence diagrams anyway. Closures in JavaScript is a bit like defining a class in Java or C#, you don't put that in your UML. Hmm, I can't explain this very well ..

Closures is something you have to inherently understand as a JavaScript programmer.

What your UML should be focusing on are the entities and their interaction. Not some language 'quirk' (if you will) like the need for closures.

I am all for describing misleading code, but a UML diagram is not the place for it. Put it in the comments in the source code. If anyone wants to know how this function works he'll look at the source code. If he doesn't want to know, don't bother him with it.

like image 64
Halcyon Avatar answered Oct 07 '22 01:10

Halcyon


I like the diagrams Dmitry Soshnikov used in JavaScript. The Core to explain execution context and scope chains. In the comments, he says they were done in Visio (not that the tool is the important thing here, it's the concepts the structures help get across).

I can see how similar diagrams could be used to demonstrate how every function created in your example code ends up accessing an i variable in the same scope, and how in a fixed version of the code, each function would be carrying around another item at the head of its scope chain, with a variable holding the current value of i at the time the containing scope was closed over.

like image 41
Jonny Buchanan Avatar answered Oct 07 '22 01:10

Jonny Buchanan


I know this is already answered, but here is a good example of using object diagrams to explain functions, closures, objects in JavaScript

https://howtonode.org/object-graphs

The graphs are built with text files (in DOT notation) and are then auto-generated using GraphViz

The author, Tim Caswell, has included links to the source files on his GitHub account

enter image description here

like image 20
Drenai Avatar answered Oct 06 '22 23:10

Drenai