Skip to content

Instantly share code, notes, and snippets.

@jgphilpott
Last active September 20, 2023 13:37
Show Gist options
  • Save jgphilpott/a876171a09ce00e450d7f4325542d819 to your computer and use it in GitHub Desktop.
Save jgphilpott/a876171a09ce00e450d7f4325542d819 to your computer and use it in GitHub Desktop.
A collection of JavaScript Boolean prototype updates.
# Info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
# Info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean
# Source: https://gist.github.com/jgphilpott/a876171a09ce00e450d7f4325542d819
Object.defineProperty Boolean.prototype, "true",
value: ->
this is true
configurable: false,
enumerable: false,
writable: false
Object.defineProperty Boolean.prototype, "false",
value: ->
this is false
configurable: false,
enumerable: false,
writable: false
Object.defineProperty Boolean.prototype, "boolean",
value: ->
Boolean this.true()
configurable: false,
enumerable: false,
writable: false
Object.defineProperty Boolean.prototype, "number",
value: ->
Number this
configurable: false,
enumerable: false,
writable: false
Object.defineProperty Boolean.prototype, "string",
value: ->
String this
configurable: false,
enumerable: false,
writable: false
Object.defineProperty Boolean.prototype, "array",
value: ->
Array this
configurable: false,
enumerable: false,
writable: false
Object.defineProperty Boolean.prototype, "object",
value: ->
Object this
configurable: false,
enumerable: false,
writable: false
// Info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
// Info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean
// Source: https://gist.github.com/jgphilpott/a876171a09ce00e450d7f4325542d819
Object.defineProperty(Boolean.prototype, "true", {
value: function() {
return this == true;
},
configurable: false,
enumerable: false,
writable: false
});
Object.defineProperty(Boolean.prototype, "false", {
value: function() {
return this == false;
},
configurable: false,
enumerable: false,
writable: false
});
Object.defineProperty(Boolean.prototype, "boolean", {
value: function() {
return Boolean(this.true());
},
configurable: false,
enumerable: false,
writable: false
});
Object.defineProperty(Boolean.prototype, "number", {
value: function() {
return Number(this);
},
configurable: false,
enumerable: false,
writable: false
});
Object.defineProperty(Boolean.prototype, "string", {
value: function() {
return String(this);
},
configurable: false,
enumerable: false,
writable: false
});
Object.defineProperty(Boolean.prototype, "array", {
value: function() {
return Array(this);
},
configurable: false,
enumerable: false,
writable: false
});
Object.defineProperty(Boolean.prototype, "object", {
value: function() {
return Object(this);
},
configurable: false,
enumerable: false,
writable: false
});
@jgphilpott
Copy link
Author

jgphilpott commented Sep 5, 2023

Prototyping

This gist is part of a collection of gists that extend JavaScript data prototypes such as Boolean, Number, String, Array and Object. See below for links to all the gists:

Note: A loose rather than strict equality check is necessary for the true/false methods to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment