Last active
September 20, 2023 13:32
-
-
Save jgphilpott/8971e9e9760895d1a87ae58e5a740f11 to your computer and use it in GitHub Desktop.
A collection of JavaScript Object prototype updates.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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/Object | |
# Source: https://gist.github.com/jgphilpott/8971e9e9760895d1a87ae58e5a740f11 | |
Object.defineProperty Object.prototype, "attach", | |
value: (path, item) -> | |
this[path] = item | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty Object.prototype, "detach", | |
value: (path) -> | |
delete this[path] | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty Object.prototype, "paths", | |
value: -> | |
Object.keys this | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty Object.prototype, "items", | |
value: -> | |
Object.values this | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty Object.prototype, "clear", | |
value: -> | |
for path in Object.keys this | |
delete this[path] | |
return this | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty Object.prototype, "empty", | |
value: -> | |
Object.keys(this).length is 0 | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty Object.prototype, "boolean", | |
value: -> | |
Boolean Object.keys(this).length | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty Object.prototype, "number", | |
value: -> | |
Number this | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty Object.prototype, "string", | |
value: -> | |
String this | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty Object.prototype, "array", | |
value: -> | |
Array this | |
configurable: false, | |
enumerable: false, | |
writable: false | |
Object.defineProperty Object.prototype, "object", | |
value: -> | |
Object this | |
configurable: false, | |
enumerable: false, | |
writable: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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/Object | |
// Source: https://gist.github.com/jgphilpott/8971e9e9760895d1a87ae58e5a740f11 | |
Object.defineProperty(Object.prototype, "attach", { | |
value: function(path, item) { | |
return this[path] = item; | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(Object.prototype, "detach", { | |
value: function(path) { | |
return delete this[path]; | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(Object.prototype, "paths", { | |
value: function() { | |
return Object.keys(this); | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(Object.prototype, "items", { | |
value: function() { | |
return Object.values(this); | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(Object.prototype, "clear", { | |
value: function() { | |
var i, len, path, ref; | |
ref = Object.keys(this); | |
for (i = 0, len = ref.length; i < len; i++) { | |
path = ref[i]; | |
delete this[path]; | |
} | |
return this; | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(Object.prototype, "empty", { | |
value: function() { | |
return Object.keys(this).length === 0; | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(Object.prototype, "boolean", { | |
value: function() { | |
return Boolean(Object.keys(this).length); | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(Object.prototype, "number", { | |
value: function() { | |
return Number(this); | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(Object.prototype, "string", { | |
value: function() { | |
return String(this); | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(Object.prototype, "array", { | |
value: function() { | |
return Array(this); | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); | |
Object.defineProperty(Object.prototype, "object", { | |
value: function() { | |
return Object(this); | |
}, | |
configurable: false, | |
enumerable: false, | |
writable: false | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prototyping
This gist is part of a collection of gists that extend JavaScript data prototypes such as
Boolean
,Number
,String
,Array
andObject
. See below for links to all the gists:Note: This prototyping gist has being designed differently because
Object
prototyping has the propensity to break some libraries such as jQuery for example.