When debugging, it would sometimes be convenient to have a function that would list all the fields and methods that an object has. I have personally wondered about the fields in the various classes the p5.js provides. The function listMembers is the result. It turns out to have more capabilities than I originally expected. It turns out that it can handle almost any globally declared variable or other item known to js.
This program allows the user to select the type of output from the function, where that information is to be displayed, the variable to be processed, and which type of member values should be included. A short explanation of each option is displayed when an item is chanaged in any of the drop down select boxes.
The listMembers(obj, returnType, includeKind) function used in this program has 1 required and 2 optional parameters.
Most users will probably want to simplify the function once they decide how it is to be used. My version of the function follows:
/** * Return a list of all of obj's members */ function listMembers(obj, returnType, includeKind) { if (returnType == null){ returnType = "code"; } if (includeKind == null) { includeKind = "all"; } var s = ""; var a = []; var count = 0; var cr; var indent = " "; if (returnType == "HTML") { cr = "<br>"; } else { cr = "\n"; } for (var p in obj) { if (includeKind == "all" || includeKind == (typeof obj[p])) { if (returnType == "array"){ a[count] = [p, obj[p], (typeof obj[p])]; } else { s += cr + indent + " " + p + ": " + obj[p] + " (" + (typeof obj[p]) + ")"; } count++; } } if (returnType == "array") { return a; } else { s = "Type of object: " + (typeof obj) + cr + "It has " + count + " " + includeKind + " members:" + s; return s; } } // listMembers
The file with the listMembers function: "http://www.brinkje.com/p5js/listMembers.pjs"
p5.js source code: HowToListMembers.pjs
James Brink 2/10/2020