mirror of
https://github.com/photonstorm/phaser
synced 2024-11-23 21:24:09 +00:00
Handy new utility class, now used by Config and Settings.
This commit is contained in:
parent
1db02f3d3a
commit
3a963905b2
1 changed files with 39 additions and 0 deletions
39
v3/src/utils/GetObjectValue.js
Normal file
39
v3/src/utils/GetObjectValue.js
Normal file
|
@ -0,0 +1,39 @@
|
|||
// Source object
|
||||
// The key as a string, or an array of keys, i.e. 'banner', or 'banner.hideBanner'
|
||||
// The default value to use if the key doesn't exist
|
||||
|
||||
var GetObjectValue = function (source, key, defaultValue)
|
||||
{
|
||||
if (key.indexOf('.'))
|
||||
{
|
||||
keys = key.split('.');
|
||||
|
||||
var parent = source;
|
||||
var value = defaultValue;
|
||||
|
||||
// Use for loop here so we can break early
|
||||
for (var i = 0; i < keys.length; i++)
|
||||
{
|
||||
if (parent.hasOwnProperty(keys[i]))
|
||||
{
|
||||
// Yes it has a key property, let's carry on down
|
||||
value = parent[keys[i]];
|
||||
|
||||
parent = parent[keys[i]];
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
else
|
||||
{
|
||||
return (source.hasOwnProperty(key) ? source[key] : defaultValue);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = GetObjectValue;
|
Loading…
Reference in a new issue