Skip to content

Instantly share code, notes, and snippets.

@JohnL4
Last active January 3, 2018 00:05
Show Gist options
  • Save JohnL4/fc460c9405e1f3b5318c58816c9da475 to your computer and use it in GitHub Desktop.
Save JohnL4/fc460c9405e1f3b5318c58816c9da475 to your computer and use it in GitHub Desktop.
Make several kinds of objects to see which is the best type for use with JSON. Includes pattern for making new "naked" objects that can be serialized as JSON.
// An experiment in making several types of objects in order to see what is the best one for use with JSON.
// Object types created are:
// - Object.create(null) - totally blank object, not even inheritance from Object (i.e., not even
// hasOwnProperty()). THIS IS PROBABLY THE WAY TO GO FOR MAKING JSON OBJECTS.
// - new Object() - inherits all base capabilities and properties
// - new Map() - can't be stringified to JSON
//-----------------------------------------------------------------------------------------------------------------
scribble("================ Object.create(null)");
let obj = Object.create(null);
obj["property A"] = "value A";
obj["property B"] = "value B";
for (const p in obj) {
// Note that hasOwnProperty() does not exist on the blank object created with Object.create(null).
// if (obj.hasOwnProperty(p))
scribble(p.toString() + ' = ' + obj[p]);
}
//-----------------------------------------------------------------------------------------------------------------
scribble("================ new Object()");
obj = new Object(); // Object.create(null);
obj["property A"] = "value A";
obj["property B"] = "value B";
for (const p in obj) {
// Note that hasOwnProperty() does not exist on the blank object created with Object.create(null).
// When taking this approach (subclassed from some Object), it is recommended that you check
// obj.hasOwnProperty( prop) to be sure you really are handling a property of the object you think you
// are, rather than some inherited property you might not be expecting. (Note that this probably won't)
// work if you're trying to be polymorphic, in which case: why are you iterating properties like this?)
if (obj.hasOwnProperty(p))
scribble(p.toString() + ' = ' + obj[p]);
}
//-----------------------------------------------------------------------------------------------------------------
scribble("================ JSON");
class NavigationSectionInfo {
constructor(public isDocumented: boolean, public subSections: Array<string>) { }
get hasSubsections(): boolean { return this.subSections != null && this.subSections.length > 0; }
}
//-----------------------------------------------------------------------------------------------------------------
let dictMap = new Map<string, NavigationSectionInfo>();
dictMap.set("Section-1", new NavigationSectionInfo(true, null));
dictMap.set("Section-2", new NavigationSectionInfo(true, ["subsection-a", "subsection-b"]));
scribble("dictMap: " + JSON.stringify(dictMap));
//-----------------------------------------------------------------------------------------------------------------
// NOTE: This is the way to go for simple JSON objects
let dictObj = Object.create(null);
dictObj["Section-1"] = new NavigationSectionInfo(true, null);
dictObj["Section=2"] = new NavigationSectionInfo(true, ["subsection-a", "subsection-b"]);
scribble("dictObj: " + JSON.stringify(dictObj));
//-----------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------
/**
* Write the given message in a PRE element of the document.
*/
function scribble(aMsg: string): void {
console.log(aMsg);
// Get a reference to the document's PRE element, or null if there isn't one.
var preElt = document.body.querySelector("pre");
if (preElt) {
// Do nothing, we're good
// console.log("got preElt");
}
else {
// console.log('make new pre elt');
preElt = document.createElement("pre");
// console.log('append pre elt to document body');
document.body.appendChild(preElt);
}
// console.log('append new Text to pre elt');
preElt.appendChild(new Text(aMsg + '\n'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment