Object Class
Pollyfils for often used functionality for objects
Item Index
Methods
deepClone
-
[descriptors=false]
-
[proto]
Returns a deep copy of the object. Only handles members of primary types, Dates, Arrays and Objects. Will clone all the properties, also the non-enumerable.
Parameters:
Returns:
deep-copy of the original
defineData
-
obj
-
[clone=false]
Sets the properties of obj
to the instance. This will redefine the object, while remaining the instance.
This way, external references to the object-instance remain valid.
each
-
fn
Loops through all properties in the object. Equivalent to Array.forEach. The callback is provided with the value of the property, the name of the property and a reference to the whole object itself. The context to run the callback in can be overriden, otherwise it is undefined.
Parameters:
-
fn
FunctionFunction to be executed on each item in the object. It will receive value {any} value of the property key {string} name of the property obj {Object} the whole of the object
empty
()
chainable
Empties the Object by deleting all its own properties (also non-enumerable).
hasKey
-
property
Checks whether the given property is a key: an enumerable property.
Parameters:
-
property
Stringthe property to check for
Returns:
Keys of the object
isEmpty
()
Boolean
Returns true if the object has no own members
Returns:
true if the object is empty
isObject
()
Boolean
static
Returns true if the item is an object, but no Array, Function, RegExp, Date or Error object
Returns:
true if the object is empty
keys
()
Array
Returns the keys of the object: the enumerable properties.
Returns:
Keys of the object
map
-
fn
Loops through the properties in an object until the callback assembling a new object
with its properties set to the values returned by the callback function.
If the callback function returns undefined
the property will not be copied to the new object.
The resulting object will have the same keys as the original, except for those where the callback
returned undefined
which will have dissapeared.
The callback is provided with the value of the property, the name of the property
and a reference to the whole object itself.
The context to run the callback in can be overriden, otherwise it is undefined.
Parameters:
-
fn
FunctionFunction to be executed on each item in the object. It will receive value {any} value of the property key {string} name of the property obj {Object} the whole of the object
Returns:
The new object with its properties set to the values returned by the callback function.
merge
-
obj
Returns a new object resulting of merging the properties of the given objects. The copying is shallow, complex properties will reference the very same object. Properties in later objects do not overwrite properties of the same name in earlier objects. If any of the objects is missing, it will be skiped.
Parameters:
-
obj
Object multipleObjects whose properties are to be merged
Returns:
new object with the properties merged in.
Example:
var foo = function (config) { config = Object.merge(config, defaultConfig); }
merge
-
obj
-
[options]
Merges into this object the properties of the given object. If the second argument is true, the properties on the source object will be overwritten by those of the second object of the same name, otherwise, they are preserved.
Parameters:
-
obj
ObjectObject with the properties to be added to the original object
-
[options]
Object optional-
[force=false]
Boolean optionalIf true, the properties in
obj
will override those of the same name in the original object -
[full=false]
Boolean optionalIf true, also any non-enumerable properties will be merged
-
[replace=false]
Boolean optionalIf true, only properties that already exist on the instance will be merged (forced replaced). No need to set force as well.
-
[descriptors=false]
Boolean optionalIf true, the full descriptors will be set. This takes more time, but avoids any info to be lost.
-
newProto
-
obj
-
proto
-
[clone=false]
Returns a new object with the prototype specified by proto
.
Parameters:
Returns:
new object with the prototype specified.
observe
()
chainable
Observes changes of the instance. On any changes, the callback will be invoked. Uses a polyfill on environments that don't support native Object.observe.
The callback comes without arguments (native Object.observe does, but non-native doesn't) so, they cannot be used.
Will observer the complete object nested (deep).
protectedProp
()
static
Creates a protected property on the object.
sameValue
-
refObj
Compares this object with the reference-object whether they have the same value. Not by reference, but their content as simple types.
Compares both JSON.stringify objects
Parameters:
-
refObj
Objectthe object to compare with
Returns:
whether both objects have the same value
shallowClone
-
[options.descriptors=false]
Returns a shallow copy of the object. It does not clone objects within the object, it does a simple, shallow clone. Fast, mostly useful for plain hash maps.
Parameters:
-
[options.descriptors=false]
Boolean optionalIf true, the full descriptors will be set. This takes more time, but avoids any info to be lost.
Returns:
shallow copy of the original
size
-
inclNonEnumerable
Returns the number of keys of the object
Parameters:
-
inclNonEnumerable
Booleanwether to include non-enumeral members
Returns:
Number of items
some
-
fn
Loops through the properties in an object until the callback function returns truish. The callback is provided with the value of the property, the name of the property and a reference to the whole object itself. The order in which the elements are visited is not predictable. The context to run the callback in can be overriden, otherwise it is undefined.
Parameters:
-
fn
FunctionFunction to be executed on each item in the object. It will receive value {any} value of the property key {string} name of the property obj {Object} the whole of the object
Returns:
true if the loop was interrupted by the callback function returning truish.
toArray
-
[options]
Transforms the object into an array with 'key/value' objects
Parameters:
Returns:
the transformed Array-representation of the object
Example:
{country: 'USA', Continent: 'North America'} --> [{key: 'country', value: 'USA'}, {key: 'Continent', value: 'North America'}]
unobserve
()
chainable
Un-observes changes that are registered with observe
.
Uses a polyfill on environments that don't support native Object.observe.