Namespace $.js.Object
Holds the methods used for manipulating Objects.
Defined in: core-js.js.
| Constructor Attributes | Constructor Name and Description |
|---|---|
|
Holds the methods used for manipulating objects.
|
| Method Attributes | Method Name and Description |
|---|---|
| <static> |
$.js.Object.every(callback, thisObject)
Check if the callback function returns true for every property
in the current object.
|
| <static> |
$.js.Object.filter(callback, thisObject)
Filter the current object using the callback function.
|
| <static> |
$.js.Object.forEach(callback, thisObject)
Execute the callback function for each property in the current
object.
|
| <static> |
$.js.Object.map(callback, thisObject)
Create a new object with the same properties as the current object using
the values returned by the callback function.
|
| <static> |
$.js.Object.reduce(callback, initialValue)
Apply the callback function to two values from the current
object, simultaneously, for the purpose of reducing the object to a single
value.
|
| <static> |
$.js.Object.some(callback, thisObject)
Check if the callback function returns true for at least one
property in the current object.
|
By default, the global Object (not the
Object.prototype!) is extended to contain all the methods in this
namespace. Besides these methods, Object will have one more
method: Object.extend, which is an alias of $.extend.
All of these methods use their this object. As such, you need to pass the correct this object.
Note that the methods provided iterate over the object properties using
for (property in this). As such, the order
depends on the browser implementation. Also, those properties which are not
enumerable
(this.propertyIsEnumerable(property)) are
skipped.
In the examples provided for each method we will assume that the objects
defined are already extended. Thus code like
obj.filter(callback) can be written
directly.
// To use these methods proceed as follows:
var obj = {'a' : 'test1', 'b' : 'test2', 'c' : 'test3'};
Object.forEach.call(obj, function (value) {
// ...
});
// Instead of forEach, you can use any of the methods defined in this
// namespace.
Additionally, you can extend your object to have all these methods:Object.extend.call(obj, Object);// After doing so, you can directly use any desired method. // For example:obj.forEach(callback).
- See:
- $.js.Array The Array functions which provide similar functionality.
The callback function is invoked for each property in the current object, and it is given three arguments: the value, the property name, and the entire object being iterated over.
Iteration over the object stops once reaching the end, or once the callback function returns false.
This method does not alter the current object.
var obj = {'a' : 10, 'b' : 11, 'c' : 12};// The following returns true.obj.every(function (value) { return value > 9; });
- Parameters:
- {Function} callback
- The callback function used for traversing the current object.
- {Object} thisObject Optional
- The object which will be the this within the scope of the callback function, for each invocation.
- Throws:
- {TypeError}
- If callback is not a function.
- Returns:
- {Boolean} False is returned if the callback returns false once. Otherwise, this method returns true.
- See:
- $.js.Array.every
The callback function is invoked for each property in the current object, and it is given three arguments: the value, the property name, and the entire object being iterated over.
This method does not alter the current object.
var obj1 = {'a' : 5, 'b' : 11, 'c' : 7};// Filter only the properties with a value lower than 10:var obj2 = obj1.filter( function (value, property, object) { return value < 10; } );// Nowobj2 = {'a' : 5, 'c' : 7}.
- Parameters:
- {Function} callback
- The callback function used for filtering the current object.
- {Object} thisObject Optional
- The object which will be the this within the scope of the callback function, for each invocation.
- Throws:
- {TypeError}
- If callback is not a function.
- Returns:
- {Object} The new object contains only the properties for which the callback function returned true.
- See:
- $.js.Array.filter
The callback function is invoked with three arguments: the value, the property name, and the entire object being iterated over.
This method does not alter the current object.
var obj = {'a' : 10, 'b' : 11, 'c' : 12};// alert() each property and value.obj.forEach( function (value, property) { alert(property + ' : ' + value); } );
- Parameters:
- {Function} callback
- The callback function used for traversing the current object.
- {Object} thisObject Optional
- The object which will be the this within the scope of the callback function, for each invocation.
- Throws:
- {TypeError}
- If callback is not a function.
- See:
- $.js.Array.forEach
The callback function is invoked for each property in the current object, and it is given three arguments: the value, the property name, and the entire object being iterated over.
This method does not alter the current object.
var obj1 = {'a' : 5, 'b' : 11, 'c' : 7};// Let's double the numbers:var obj2 = obj1.map(function (value) { return value * 2; });// Nowobj2 = {'a' : 10, 'b' : 22, 'c' : 14}.
- Parameters:
- {Function} callback
- The callback function used for traversing the current object.
- {Object} thisObject Optional
- The object which will be the this within the scope of the callback function, for each invocation.
- Throws:
- {TypeError}
- If callback is not a function.
- Returns:
- {Object} The new object has the same properties as the current object, but the values are those returned by the callback function.
- See:
- $.js.Array.map
The callback function is called for each property in the current object, and it is given four arguments: the previous value, the current value, the current property name, and the entire object being iterated over.
This method does not alter the current object.
Note that the execution order is the same as doing for (prop in
obj).
var obj = {'a' : 5, 'b' : 10, 'c' : 6};// Let's calculate the sum:var result = obj.reduce( function (sum, value) { return sum + value; }, 0);// Now result = 21.
- Parameters:
- {Function} callback
- The callback function used for traversing the current object.
- initialValue Optional
- The initial value used when the callback function is first invoked. If the initial value is not provided, then the method uses the value of the first property found in the object being iterated.
- Throws:
- {TypeError}
- If callback is not a function.
- Returns:
- The result of the last callback function invocation.
The callback function is invoked for each property in the current object, and it is given three arguments: the value, the property name, and the entire object being iterated over.
Iteration over the object stops once reaching the end, or once the callback function returns true.
This method does not alter the current object.
var obj = {'a' : 10, 'b' : 11, 'c' : 12};// The following returns true.obj.some(function (value) { return value > 11; });
- Parameters:
- {Function} callback
- The callback function used for traversing the current object.
- {Object} thisObject Optional
- The object which will be the this within the scope of the callback function, for each invocation.
- Throws:
- {TypeError}
- If callback is not a function.
- Returns:
- {Boolean} True is returned if the callback returns true once. Otherwise, this method returns false.
- See:
- $.js.Array.some