Skip to content

Instantly share code, notes, and snippets.

@eduardolundgren
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eduardolundgren/4733f405860b2e2c4deb to your computer and use it in GitHub Desktop.
Save eduardolundgren/4733f405860b2e2c4deb to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
function createDummyNullable(target) {
return new Proxy(target, {
get: function(wrap, name) {
return wrap[name] === undefined ? null : wrap[name];
}
});
}
var content = createDummyNullable({
key1: 1,
key2: 2
});
content.key3 = 3;
console.log(content.key1); // 1
console.log(content.key2); // 2
console.log(content.key3); // 3
console.log(content.doesNotExist); // null
console.log(content.toString); // Keep existing
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment