ProAct.Utils Class
src/js/core/pro.js:156
Contains a set of utility functions to ease working with arrays and objects.
Can be reffered by using ProAct.U
too.
This class is part of the proact-core
module of ProAct.js.
Item Index
Methods
bind
-
ctx
-
func
Binds a function
to an object context
.
Every time the function
is called, this
will point to the passed object
.
var context = {a: 3};
var f = ProAct.Utils.bind(context, function () {
return this;
});
var result = f();
console.log(result === context); // prints 'true'
Parameters:
Returns:
The bound function
.
clone
-
obj
Clones the passed object. It creates a deep copy of it. For now it clones only arrays.
TODO It is not fully implemented...
Parameters:
-
obj
ObjectThe object to clone.
Returns:
Clone of the passed object.
defValProp
-
obj
-
prop
-
enumerable
-
configurable
-
writable
-
val
Defines a property to an object that contains a initial value.
The property can be configured using the arguments passed if it is possible in the javascript implementation.
Parameters:
-
obj
ObjectThe object to define a property in.
-
prop
StringThe name of the property to define.
-
enumerable
BooleanIf the property should be enumerable.
In other words visible when doingfor (p in obj) {}
-
configurable
BooleanIf the property should be configurable.
In other words if the parameters of the property for example enumerable or writable can be changed in the future. -
writable
BooleanIf the property can be changed.
-
val
ObjectThe initial value of the property.
diff
-
array1
-
array2
A powerful function that creates a diff object containing the differences between two arrays.
Returns:
The object returned contains a property for every index there is a difference between the passed arrays.
The object set on the index has two array properties : 'o' and 'n'.
The 'o' property represents the owned elemetns of the first array that are different from the other's.
The 'n' property contains all the elements that are not owned by the first array, but present in the other.
Example:
var array1 = [1, 3, 4, 5],
array2 = [1, 2, 7, 5, 6]
diff;
diff = ProAct.Utils.diff(array1, array2);
console.log(diff[0]); // undefined - the arrays are the same at he index 0
console.log(diff[1]); // {o: [3, 4], n: [2, 7]}
console.log(diff[2]); // undefined the change began from index 1, so it is stored there
console.log(diff[3]); // undefined - the arrays are the same at index 3
console.log(diff[4]); // {o: [], n: [6]}
ex
-
destination
-
source
Extends the destination object with the properties and methods of the source object.
var obj1 = {a: 3};
var obj2 = {b: 4;}
ProAct.Utils.ex(obj2, obj1);
console.log(obj2);
// This prints : {a: 3, b: 4}
Parameters:
Returns:
The changed destination object.
extendClass
-
data
Used for extending of classes. Example is:
var Bar = ProAct.Utils.extendClass.call(Foo, {
a: 1,
b: 2,
c: function () {}
});
Parameters:
-
data
ObjectData to add new properties to the new class or override old ones.
Returns:
Child class.
isArray
-
value
Checks if the passed value is a valid JavaScript Array instance or not.
Parameters:
-
value
ObjectThe value to check.
Returns:
True if the passed value
is Array.
isEmptyObject
-
value
Checks if the passed value is {} or not.
Parameters:
-
value
ObjectThe value to check.
Returns:
True if the value is object that has no own fields.
isError
-
value
Checks if the passed value is a valid JavaScript Error instance or not.
Parameters:
-
value
ObjectThe value to check.
Returns:
True if the passed value
is instance of an Error.
isFunction
-
value
Checks if the passed value is a Function or not.
Parameters:
-
value
ObjectThe object/value to check.
Returns:
True if the passed value is a Function.
isObject
-
value
Checks if the passed value is a JavaScript object or not.
Parameters:
-
value
ObjectThe value to check.
Returns:
True if the passed values is not primitive.
isString
-
value
Checks if the passed value is a String instance or not.
Parameters:
-
value
ObjectThe object/value to check.
Returns:
True if the passed value is a String.
remove
-
array
-
value
Removes the first appearance of the passed value
in the passed array
.
If the value
is not present in the passed array
does nothing.
var array = [1, 2, 3];
ProAct.Utils.remove(array, 2);
console.log(array); // prints [1, 3]