From 55371f9c78e3412707aa5368e1debf939c1cedae Mon Sep 17 00:00:00 2001 From: Michael Teeuw Date: Thu, 13 Oct 2016 14:25:53 +0200 Subject: [PATCH] Improve object instantiation to prevent reference errors. --- js/class.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/js/class.js b/js/class.js index 9d04944e..2b011b29 100644 --- a/js/class.js +++ b/js/class.js @@ -11,6 +11,24 @@ // The base Class implementation (does nothing) this.Class = function() {}; + //Define the clone method for later use. + function cloneObject(obj) { + if (obj === null || typeof obj !== "object") { + return obj; + } + + var temp = obj.constructor(); // give temp the original obj's constructor + for (var key in obj) { + temp[key] = cloneObject(obj[key]); + + if (key === "lockStrings") { + Log.log(key); + } + } + + return temp; + } + // Create a new Class that inherits from this class Class.extend = function(prop) { var _super = this.prototype; @@ -21,6 +39,11 @@ var prototype = new this(); initializing = false; + // Make a copy of all prototype properies, to prevent reference issues. + for (var name in prototype) { + prototype[name] = cloneObject(prototype[name]); + } + // Copy the properties over onto the new prototype for (var name in prop) { // Check if we're overwriting an existing function