mirror of
https://github.com/wassname/phaser.git
synced 2026-07-01 16:50:43 +08:00
Moving to using a small extend / mixin method to avoid code repeating now we're extending Pixi display objects.
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
Phaser.Utils = {
|
||||
|
||||
// This is a slightly modified version of jQuery.isPlainObject
|
||||
isPlainObject: function (obj) {
|
||||
|
||||
// Not plain objects:
|
||||
// - Any object or value whose internal [[Class]] property is not "[object Object]"
|
||||
// - DOM nodes
|
||||
// - window
|
||||
if (typeof(obj) !== "object" || obj.nodeType || obj === obj.window)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Support: Firefox <20
|
||||
// The try/catch suppresses exceptions thrown when attempting to access
|
||||
// the "constructor" property of certain host objects, ie. |window.location|
|
||||
// https://bugzilla.mozilla.org/show_bug.cgi?id=814622
|
||||
try {
|
||||
if (obj.constructor && !hasOwn.call(obj.constructor.prototype, "isPrototypeOf"))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If the function hasn't returned already, we're confident that
|
||||
// |obj| is a plain object, created by {} or constructed with new Object
|
||||
return true;
|
||||
},
|
||||
|
||||
// deep, target, objects to copy to the target object
|
||||
// This is a slightly modified version of jQuery.extend (http://api.jquery.com/jQuery.extend/)
|
||||
extend: function () {
|
||||
|
||||
var options, name, src, copy, copyIsArray, clone,
|
||||
target = arguments[0] || {},
|
||||
i = 1,
|
||||
length = arguments.length,
|
||||
deep = false;
|
||||
|
||||
// Handle a deep copy situation
|
||||
if (typeof target === "boolean")
|
||||
{
|
||||
deep = target;
|
||||
target = arguments[1] || {};
|
||||
// skip the boolean and the target
|
||||
i = 2;
|
||||
}
|
||||
|
||||
// extend Phaser if only one argument is passed
|
||||
if (length === i)
|
||||
{
|
||||
target = this;
|
||||
--i;
|
||||
}
|
||||
|
||||
for ( ; i < length; i++ )
|
||||
{
|
||||
// Only deal with non-null/undefined values
|
||||
if ((options = arguments[i]) != null)
|
||||
{
|
||||
// Extend the base object
|
||||
for (name in options)
|
||||
{
|
||||
src = target[name];
|
||||
copy = options[name];
|
||||
|
||||
// Prevent never-ending loop
|
||||
if (target === copy)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Recurse if we're merging plain objects or arrays
|
||||
if (deep && copy && (Phaser.Utils.isPlainObject(copy) || (copyIsArray = Array.isArray(copy))))
|
||||
{
|
||||
if (copyIsArray)
|
||||
{
|
||||
copyIsArray = false;
|
||||
clone = src && Array.isArray(src) ? src : [];
|
||||
}
|
||||
else
|
||||
{
|
||||
clone = src && Phaser.Utils.isPlainObject(src) ? src : {};
|
||||
}
|
||||
|
||||
// Never move original objects, clone them
|
||||
target[name] = extend(deep, clone, copy);
|
||||
|
||||
// Don't bring in undefined values
|
||||
}
|
||||
else if (copy !== undefined)
|
||||
{
|
||||
target[name] = copy;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return the modified object
|
||||
return target;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user