/** * @module listMembers */ /** * Returns a list of the obj's members. The format depends on the options * selected. Each entry includes the member name, value, and the type of * that value. The concept is most easily thought of providing a list of * the members of a class but the function can be applied to almost any type * of variable or object. See the example for the listMembersTable function. * * This method does not require p5.js functions. * @method listMembers * @param {object} obj an object, string, number, function, ... or just * about anything with a name or value. * @param {string} [includeKind] Specifies what type of elements to include. * These types are those returned by the Javascript typeof * function. (See * https://developer.mozilla.org/en-US/docs/Web/ JavaScript/Reference/Operators/typeof * Options: * "all" (the default) Specifies if "all" items will be * included, * "undefined" (items that have not been given any value), * "null" * "boolean", * "number", * "bigint", * "string", * "symbol", * "function" (includes class definitions) (functions often * include the code as the value in line with the way that * JavaScript allows using a function as a variable.), * "object" (includes arrays and null.) * @param {string} [returnType] Determines the type output to be returned. * Options: * "code" (the default) returns a string using "\n" for a new * line to separate items in the list, The resulting string * is suitable for p5.js text, alerts, and the console. * "HTML" similar to code except it uses "
" for new lines. The * resulting string is suitable for HTML components. * (See also: the "array" output option) * @return {string} For each member in the list, the string contains the * name, value, and the type of the value. Elements are separated * by "\n" (default) or "
" as specified by the returnType * parameter. */ /** * @method listMembers * @param {object} obj * @param {string} [includeKind] * @param {string} [returnType] Determines the type of return value wanted. * Option: "array" returns a 3 column array with "Name", "Value" and * "Type of Value". Each item in the list is stored in * its own row. The array can be processed in any way * appropriate to arrays in p5.js (i.e. Javascript). * * @return A three column array as described above. */ function listMembers (obj, includeKind, returnType,) { if (returnType == null) { returnType = "code"; } if (includeKind == null) { includeKind = "all"; } var s = ""; // for string output var a = []; // for array output var cr; // variables used with string output var indent = " "; if (returnType == "HTML") { cr = "
"; } else { cr = "\n"; } // Loop though all members var count = 0; 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++; } } // Return appropriate item if (returnType == "array") { return a; } else { s = "Type of object: " + (typeof obj) + cr + "It has " + count + " " + includeKind + " members:" + s; return s; } } // listMembers /** * Outputs the some information about 'obj' in a string ready to be printed in * a neatly formatted HTML table of members (assuming the object has some * members). The user can print the string in an HTML component. This * function does not require p5.js functions. See the example for the * listMembersTable function. * @method listMembersTable * @param {object} obj an object, string, number, function, ... or just * about anything with a name. * @param {string} [returnType] Determines the type of members to be included * in the list. The type can be "all" or any of the values that can * be returned by the JavaScript typeof function. (A listing is * included in the description of the listMembers function.) * */ function listMembersHTML(obj, kind) { if (kind == null){ kind = "all"; } let listResults = listMembers(obj, kind, "array"); let tableStyle = "border-collapse: collapse; border: 1px solid black;" + "background-color: #EEFFEE"; let data = ''; let header = ''; let st = "Type of object: " + typeof obj; st += "
Show " + kind + " members"; st += "
Number of " + kind + " values: " + listResults.length; if (listResults.length > 0) { st += ''; st += '' + header + 'Name' + header + 'Value' + header + 'Type of Value'; for (let i = 0; i < listResults.length; i++) { st += "" + data + listResults[i][0] + "" + data + listResults[i][1] + "" + data + listResults[i][2] + ""; } st += "
"; } return st; } /** * Outputs the some information about 'obj' followed by a neatly formatted * HTML table of members (assuming the object has some members). This is * printed as a new division. This function requires p5.js functions. * @method listMembersTable * @param {object} obj an object, string, number, function, ... or just * about anything with a name. * @param {string} [returnType] Determines the type of members to be included * in the list. The type can be "all" or any of the values that can * be returned by the JavaScript typeof function. (A listing is * included in the description of the listMembers function.) (Optional) * * @example *
* * let e; * function setup() { * createCanvas(250, 140); * class Example { * constructor () { * this.f = function(x) {return 10 * x;}; * this.s = "a string"; * this.u; * this.a = ["abc", 25, this.f, this.u, true, this.s]; * } * } * e = new Example(); * createSpan('
The object: e (an object in a class)'); * listMembersTable(e); * listMembersTableTitle("Members of e", "number")/; * lm = listMembersHTML(e.a); * createDiv('The object: e.a (an array)' + lm); * alert("Members of e\n\n" + listMembers(e)); * alert("Function members of object e\n\n" * + listMembers(e, "function", "code")); * print(listMembers(e)); // output sent to the console * } * function draw() { * background(128); * fill(255, 255, 0); * text(listMembers(e.a), 5, 20); * } *
*
*/ function listMembersTable(obj, kind) { createDiv(listMembersHTML(obj, kind)); } /** * Outputs the title in an <h3>: header followed by some information * about 'obj' followed by a neatly formatted * HTML table of members (assuming the object has some members). This is * printed as a new division. This function requires p5.js functions. * @method listMembersTable * @param {string} the title for the table * @param {object} obj an object, string, number, function, ... or just * about anything with a name. * @param {string} [returnType] Determines the type of members to be included * in the list. The type can be "all" or any of the values that can * be returned by the JavaScript typeof function. (A listing is * included in the description of the listMembers function.) (Optional) */ function listMembersTableTitle(title, obj, kind) { createDiv("

" + title + "

"+ listMembersHTML(obj, kind)); } /* let e; function setup() { createCanvas(300, 140); class Example { constructor () { this.f = function(x) {let y = 10 * x; return y;}; this.s = "a string"; this.u; this.a = ["abc", 25, this.f, this.u, true, this.s]; } } e = new Example(); createSpan('
The object: e (an object in a class)'); listMembersTable(e); createSpan('The object: e.a (an array)'); listMembersTable(e.a); alert("Members of e\n\n" + listMembers(e)); alert("Function members of object e\n\n" + listMembers(e,, "function" "code")); print(listMembers(e)); // output sent to the console } function draw() { background(128); fill(255, 255, 0); text(listMembers(e.a), 5, 20); } */