API Docs for: 0.0.1
Show:

Object Class

Module: lib/object.js
Parent Module: js-ext

Pollyfils for often used functionality for objects

Methods

deepClone

(
  • [descriptors=false]
  • [proto]
)
Object

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:

  • [descriptors=false] Boolean optional

    If true, the full descriptors will be set. This takes more time, but avoids any info to be lost.

  • [proto] Object optional

    Another prototype for the new object.

Returns:

Object:

deep-copy of the original

defineData

(
  • obj
  • [clone=false]
)
chainable

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.

Parameters:

  • obj Object

    the Object that holds the new properties.

  • [clone=false] Boolean optional

    whether the properties should be cloned

each

(
  • fn
)
chainable

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 Function

    Function 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
)
Boolean

Checks whether the given property is a key: an enumerable property.

Parameters:

  • property String

    the property to check for

Returns:

Boolean:

Keys of the object

isEmpty

() Boolean

Returns true if the object has no own members

Returns:

Boolean:

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:

Boolean:

true if the object is empty

keys

() Array

Returns the keys of the object: the enumerable properties.

Returns:

Array:

Keys of the object

map

(
  • fn
)
Object

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 Function

    Function 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:

Object:

The new object with its properties set to the values returned by the callback function.

merge

(
  • obj
)
Object static

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 multiple

    Objects whose properties are to be merged

Returns:

Object:

new object with the properties merged in.

Example:

var foo = function (config) { config = Object.merge(config, defaultConfig); }

merge

(
  • obj
  • [options]
)
chainable

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 Object

    Object with the properties to be added to the original object

  • [options] Object optional
    • [force=false] Boolean optional

      If true, the properties in obj will override those of the same name in the original object

    • [full=false] Boolean optional

      If true, also any non-enumerable properties will be merged

    • [replace=false] Boolean optional

      If true, only properties that already exist on the instance will be merged (forced replaced). No need to set force as well.

    • [descriptors=false] Boolean optional

      If true, the full descriptors will be set. This takes more time, but avoids any info to be lost.

newProto

(
  • obj
  • proto
  • [clone=false]
)
Object static

Returns a new object with the prototype specified by proto.

Parameters:

  • obj Object

    source Object

  • proto Object

    Object that should serve as prototype

  • [clone=false] Boolean optional

    whether the sourceobject should be deep-cloned. When false, the properties will be merged.

Returns:

Object:

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
)
Boolean

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 Object

    the object to compare with

Returns:

Boolean:

whether both objects have the same value

shallowClone

(
  • [options.descriptors=false]
)
Object

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 optional

    If true, the full descriptors will be set. This takes more time, but avoids any info to be lost.

Returns:

Object:

shallow copy of the original

size

(
  • inclNonEnumerable
)
Number

Returns the number of keys of the object

Parameters:

  • inclNonEnumerable Boolean

    wether to include non-enumeral members

Returns:

Number:

Number of items

some

(
  • fn
)
Boolean

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 Function

    Function 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:

Boolean:

true if the loop was interrupted by the callback function returning truish.

toArray

(
  • [options]
)
Array

Transforms the object into an array with 'key/value' objects

Parameters:

  • [options] Object optional
    • [key] String optional

      to overrule the default key-property-name

    • [value] String optional

      to overrule the default value-property-name

Returns:

Array:

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.

values

() Array

Loops through the object collection the values of all its properties. It is the counterpart of the keys.

Returns:

Array:

values of the object