Skip to content

ExperimentalCyborg/bonnefooi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Bonnefooi

This npm package lets you access nonexistent object properties in javascript.

Bonnefooi is Dutch for going on a journey without planning ahead.

Usage

npm install bonnefooi

When you try to access a nonexistent member, it will automatically be initialized as an empty object. This means you can safely do this:

const bonnefooi = require('bonnefooi');

let myobj = {};
let myobjSafe = new bonnefooi(myobj);

myobjSafe.something.somethingelse.importantnumber = 123;
console.log(myobj); // Output: { something: { somethingelse: { importantnumber: 123 } } }

And you can even do this:

const bonnefooi = require('bonnefooi');

let myobjSafe = new bonnefooi({});

myobjSafe.something.somethingelse.importantnumber = 123;
console.log(myobjSafe); // Output: { something: { somethingelse: { importantnumber: 123 } } }

What it can't do

Beware that some code requires nonexistent properties to return undefined. For example, this will result in a stack overflow:

const bonnefooi = require('bonnefooi');

let myobjSafe = new bonnefooi({});

myobjSafe.something.somethingelse.importantnumber = 123;
JSON.stringify(myobjSafe); // infinite recursion

But this works fine:

const bonnefooi = require('bonnefooi');

let myobj = {};
let myobjSafe = new bonnefooi(myobj);

myobjSafe.something.somethingelse.importantnumber = 123;
JSON.stringify(myobj); // String: { something: { somethingelse: { importantnumber: 123 } } }

myobjSafe.hasOwnProperty() also does not work, because it's a proxy object which doesn't have an object prototype. If you want to check whether a property exists or not, refer to the original object, or use in:

const bonnefooi = require('bonnefooi');

let myobjSafe = new bonnefooi({});
console.log('nonexistentproperty' in myobjSafe); // Output: false

License

GNU GPLv3

About

A Javascript package for accessing nonexistent object properties.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published