Skip to main content
Version: Phaser v4.0.0

Phaser.Utils.Objects

Scope: static

Source: src/utils/object/index.js#L7

Static functions

Clone

<static> Clone(obj)

Description:

Shallow Object Clone. Will not clone nested objects.

Parameters:

nametypeoptionaldescription
objobjectNoThe object to clone.

Returns: object - A new object with the same properties as the input object.

Source: src/utils/object/Clone.js#L7
Since: 3.0.0


DeepCopy

<static> DeepCopy(inObject)

Description:

Recursively deep copies the given object or array, returning a brand new instance with all nested objects and arrays also cloned. The original object is not modified. If a non-object value is passed (such as a string, number, boolean, or null), it is returned as-is.

Parameters:

nametypeoptionaldescription
inObjectobjectNoThe object or array to deep copy.

Returns: object - A deep copy of the original object or array.

Source: src/utils/object/DeepCopy.js#L7
Since: 3.50.0


Extend

<static> Extend([args])

Description:

Merges the properties of one or more source objects into a target object, returning the modified target. This is a slightly modified version of jQuery.extend.

The function accepts a variadic argument list. If the first argument is a boolean true, a deep (recursive) copy is performed, and the second argument is treated as the target. Otherwise, the first argument is the target and all subsequent arguments are source objects. Properties from each source object are copied onto the target in order; later sources overwrite earlier ones for the same key. Nested plain objects and arrays are cloned rather than referenced during a deep copy, preventing mutation of the originals. undefined values on a source are never copied to the target.

Parameters:

nametypeoptionaldescription
args*YesAn optional leading boolean for deep-copy mode, followed by the target object, then one or more source objects whose properties will be merged into the target.

Returns: object - The extended object.

Source: src/utils/object/Extend.js#L13
Since: 3.0.0


GetAdvancedValue

<static> GetAdvancedValue(source, key, defaultValue)

Description:

Retrieves a value from an object. Extends GetValue with support for dynamic and randomized value types, making it useful for configuration objects where values may be fixed, procedural, or randomly selected at the point of use (for example, particle emitter configs or group configs).

The following value types are supported for the retrieved property:

Explicit value:

{

x: 4

}

Function — invoked with the property key as its argument; its return value is used:

{

x: function (key) { return 4; }

}

Array — one element is picked at random from the array:

{

x: [a, b, c, d, e, f]

}

Random integer between min and max (inclusive):

{

x: { randInt: [min, max] }

}

Random float between min and max:

{

x: { randFloat: [min, max] }

}

Parameters:

nametypeoptionaldescription
sourceobjectNoThe object to retrieve the value from.
keystringNoThe name of the property to retrieve from the object. If a property is nested, the names of its preceding properties should be separated by a dot (.) - banner.hideBanner would return the value of the hideBanner property from the object stored in the banner property of the source object.
defaultValue*NoThe value to return if the key isn't found in the source object.

Returns: * - The value of the requested key.

Source: src/utils/object/GetAdvancedValue.js#L10
Since: 3.0.0


GetFastValue

<static> GetFastValue(source, key, [defaultValue])

Description:

Retrieves the value of the given key from the top level of the source object. If the source is not an object (i.e. it is falsy, a number, or a string), or if the key does not exist on the source, the defaultValue is returned instead.

Parameters:

nametypeoptionaldescription
sourceobjectNoThe object to search
keystringNoThe key for the property on source. Must exist at the top level of the source object (no periods)
defaultValue*YesThe default value to use if the key does not exist.

Returns: * - The value if found; otherwise, defaultValue (null if none provided)

Source: src/utils/object/GetFastValue.js#L7
Since: 3.0.0


GetMinMaxValue

<static> GetMinMaxValue(source, key, min, max, defaultValue)

Description:

Retrieves a numerical value from a source object using the given key, then clamps it to the inclusive range defined by min and max. If the property does not exist on the source object, the defaultValue is used instead. Nested properties can be accessed by separating key names with a dot. The returned value is always within the specified bounds.

Parameters:

nametypeoptionaldescription
sourceobjectNoThe object to retrieve the value from.
keystringNoThe name of the property to retrieve from the object. If a property is nested, the names of its preceding properties should be separated by a dot (.).
minnumberNoThe minimum value which can be returned.
maxnumberNoThe maximum value which can be returned.
defaultValuenumberNoThe value to return if the property doesn't exist. If not provided, defaults to min. It's also constrained to the given bounds.

Returns: number - The clamped value from the source object.

Source: src/utils/object/GetMinMaxValue.js#L10
Since: 3.0.0


GetValue

<static> GetValue(source, key, defaultValue, [altSource])

Description:

Retrieves a value from an object, or an alternative object, falling to a back-up default value if not found.

The key is a string, which can be split based on the use of the period character.

For example:


const source = {

lives: 3,

render: {

screen: {

width: 1024

}

}

}



const lives = GetValue(source, 'lives', 1);

const width = GetValue(source, 'render.screen.width', 800);

const height = GetValue(source, 'render.screen.height', 600);

In the code above, lives will be 3 because it's defined at the top level of source. The width value will be 1024 because it can be found inside the render.screen object. The height value will be 600, the default value, because it is missing from the render.screen object.

Parameters:

nametypeoptionaldescription
sourceobjectNoThe primary object to try to retrieve the value from. If not found in here, altSource is checked.
keystringNoThe name of the property to retrieve from the object. If a property is nested, the names of its preceding properties should be separated by a dot (.) - banner.hideBanner would return the value of the hideBanner property from the object stored in the banner property of the source object.
defaultValue*NoThe value to return if the key isn't found in the source object.
altSourceobjectYesAn alternative object to retrieve the value from. If the property exists in source then altSource will not be used.

Returns: * - The value of the requested key.

Source: src/utils/object/GetValue.js#L7
Since: 3.0.0


HasAll

<static> HasAll(source, keys)

Description:

Verifies that an object contains all requested keys

Parameters:

nametypeoptionaldescription
sourceobjectNoAn object on which to check for key existence.
keysArray.<string>NoAn array of keys to ensure the source object contains.

Returns: boolean - true if the source object contains all keys, false otherwise.

Source: src/utils/object/HasAll.js#L7
Since: 3.0.0


HasAny

<static> HasAny(source, keys)

Description:

Verifies that an object contains at least one of the requested keys

Parameters:

nametypeoptionaldescription
sourceobjectNoAn object on which to check for key existence.
keysArray.<string>NoAn array of keys to search the object for.

Returns: boolean - true if the source object contains at least one of the keys, otherwise false.

Source: src/utils/object/HasAny.js#L7
Since: 3.0.0


HasValue

<static> HasValue(source, key)

Description:

Determine whether the source object has its own property with the specified key. This uses hasOwnProperty internally, so only own (non-inherited) properties are checked.

Parameters:

nametypeoptionaldescription
sourceobjectNoThe source object to be checked.
keystringNoThe property to check for within the object.

Returns: boolean - true if the provided key exists on the source object, otherwise false.

Source: src/utils/object/HasValue.js#L7
Since: 3.0.0


IsPlainObject

<static> IsPlainObject(obj)

Description:

This is a slightly modified version of jQuery.isPlainObject. A plain object is an object whose internal [[Class]] property is [object Object], meaning it was created via {}, new Object(), or Object.create(null). Objects such as DOM nodes, the window object, and instances of custom classes are not considered plain objects and will cause this function to return false.

Parameters:

nametypeoptionaldescription
objobjectNoThe object to inspect.

Returns: boolean - true if the object is plain, otherwise false.

Source: src/utils/object/IsPlainObject.js#L7
Since: 3.0.0


Merge

<static> Merge(obj1, obj2)

Description:

Performs a shallow merge of two plain objects, returning a brand new object that contains all properties from both. Neither input object is modified. When the same key exists in both objects, the value from obj1 takes precedence over the value from obj2.

This is a shallow copy only. Deeply nested objects are not cloned, so be sure to only use this function on objects with a single level of nesting.

Parameters:

nametypeoptionaldescription
obj1objectNoThe base object. Its properties take precedence in the event of a key conflict.
obj2objectNoThe secondary object. Properties unique to this object are added to the result.

Returns: object - A new object containing the merged properties of obj1 and obj2.

Source: src/utils/object/Merge.js#L9
Since: 3.0.0


MergeRight

<static> MergeRight(obj1, obj2)

Description:

Creates a new object by cloning obj1, then merging values from obj2 into it using a "right wins" strategy.

Only keys that exist in obj1 are considered. For each key in obj2 that also exists in obj1, the value from obj2 overwrites the cloned value. Keys present in obj2 but absent from obj1 are ignored entirely. This differs from a standard merge in that obj2 cannot introduce new keys — it can only override existing ones.

Parameters:

nametypeoptionaldescription
obj1objectNoThe first object to merge.
obj2objectNoThe second object to merge. Keys from this object which also exist in obj1 will be copied to obj1.

Returns: object - The merged object. obj1 and obj2 are not modified.

Source: src/utils/object/MergeRight.js#L9
Since: 3.0.0


Pick

<static> Pick(object, keys)

Description:

Returns a new object that only contains the keys that were found on the object provided. If no keys are found, an empty object is returned.

Parameters:

nametypeoptionaldescription
objectobjectNoThe object to pick the provided keys from.
keysarrayNoAn array of properties to retrieve from the provided object.

Returns: object - A new object that only contains the keys that were found on the provided object. If no keys were found, an empty object will be returned.

Source: src/utils/object/Pick.js#L9
Since: 3.18.0


SetValue

<static> SetValue(source, key, value)

Description:

Sets a value in an object, allowing for dot notation to control the depth of the property.

For example:


var data = {

world: {

position: {

x: 200,

y: 100

}

}

};



SetValue(data, 'world.position.y', 300);



console.log(data.world.position.y); // 300

Parameters:

nametypeoptionaldescription
sourceobjectNoThe object to set the value in.
keystringNoThe name of the property in the object. If a property is nested, the names of its preceding properties should be separated by a dot (.)
valueanyNoThe value to set into the property, if found in the source object.

Returns: boolean - true if the property key was valid and the value was set, otherwise false.

Source: src/utils/object/SetValue.js#L7
Since: 3.17.0