Mangler.mergeType() - DarthJDG/Mangler.js GitHub Wiki

Merge handler bindings to add/remove features.

Mangler.mergeType(type, options)
Parameter Type Default Description
type Instance
Function
An object instance or its constructor to add/remove handlers for.
options Object Handler functions to register.

Returns

true if successful. This function always succeeds, invalid handlers are ignored.

Options

Same as the options parameter of Mangler.registerType().

Property Type Default Description
$constructor Function The constructor function for the object. Optional, unless clone is set to 'constructor'.
clone Function
String
true
Optional. A function that clones the passed object of the specified type and returns the clone. Parameters: function(object). Can have the value of true if object implements a standard .clone() method. Can have the value of 'contructor' if the object has a copy-constructor.
each Function
String
true
Optional. A function that iterates through the items of the passed object of the specified type and calls a callback function. Parameters: function(object, callback). Callback parameters: function(key, value). Implementation must stop the iteration if the callback function returns false. Can have the value of true if object implements a standard .each() method. Can have the value of 'array' if object is array-like.
get Function
String
true
Optional. A function that gets and returns an item from the passed object with a specified index or key. Parameters: function(object, key). Can have the value of true if object implements a standard .get() method. Can have the value of 'array' if the object is array-like.

It is used to add/remove features of an existing type registration. All parameters are optional and will be directly merged with already registered options. To remove a certain feature, set the option's value to null.

The following example adds a simple iterator to String objects, which is registered by the Mangler-natives module. If type is not yet registered, Mangler.mergeType() will have the same effect as Mangler.registerType().

Mangler.mergeType(String, { each: function(obj, callback) {
	for(var i = 0; i < obj.length; i++) {
		if(callback(i, obj.charAt(i)) === false) return;
	}
});

To prevent cloning of Date object (built-in feature):

Mangler.mergeType(Date, { clone: null });
⚠️ **GitHub.com Fallback** ⚠️