Improve object instantiation to prevent reference errors.

This commit is contained in:
Michael Teeuw 2016-10-13 14:25:53 +02:00
parent af63f4098f
commit 55371f9c78

View File

@ -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