strict-object
A utility that makes it safer and prettier to deal with javascript objects that have a defined set of properties.
Installing
strict-object is compatable with npm, browsers, windows script host,and probably more.
npm install strict-object
When minified, strict-object.js ends up less than 400 bytes.
What
// Create a new person typevar Person = StrictObject; // Instatiate a Personvar pete = ; // set properties using functionspetename'Peter'; // get properties using functionsconsole; // property setters return the target object and are thus chainablepetename'Peter' ; // retrieve the properties as an objectconsole; // {name: 'Peter', age: 26, country: 'Uzbekistan'} // Instatiate a Person with initial valuesvar pete = name: "Peter" age: 26 country: "Uzbekawhat?";
Running Tests
npm test
Why
Because typos are too easy with basic objects.
var thing = {};thingdescriptivePropertyName = 'value';console; // null -- because of a typo.
Syntax is ugly with basic objects
var thing = key: "value" key2: "value2";
Cleaner and safer with strict-object
thing = ;
License
No License, the content of this repository is in the public domain, no attribution is necessary.