Files
MagicMirror/js/class.js
T

120 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-05-03 18:59:26 +02:00
/* global Class, xyz */
2024-08-12 22:52:43 +02:00
/*
* Simple JavaScript Inheritance
2020-04-28 23:05:28 +02:00
* By John Resig https://johnresig.com/
2020-05-03 18:59:26 +02:00
*
* Inspired by base2 and Prototype
*
2016-03-24 17:19:32 +01:00
* MIT Licensed.
*/
2017-07-17 14:23:24 +02:00
(function () {
2021-07-02 17:24:29 +02:00
let initializing = false;
2024-01-01 15:38:08 +01:00
const fnTest = (/xyz/).test(function () {
2020-05-11 22:22:32 +02:00
xyz;
})
? /\b_super\b/
: /.*/;
2016-04-03 19:52:13 +02:00
2016-04-05 14:35:11 -04:00
// The base Class implementation (does nothing)
2020-05-11 22:22:32 +02:00
this.Class = function () {};
2016-04-03 19:52:13 +02:00
2016-04-05 14:35:11 -04:00
// Create a new Class that inherits from this class
2017-07-17 14:23:24 +02:00
Class.extend = function (prop) {
2021-07-02 17:24:29 +02:00
let _super = this.prototype;
2016-04-03 19:52:13 +02:00
2024-08-12 22:52:43 +02:00
/*
* Instantiate a base class (but only create the instance,
* don't run the init constructor)
*/
2016-04-05 14:35:11 -04:00
initializing = true;
2021-07-02 17:24:29 +02:00
const prototype = new this();
2016-04-05 14:35:11 -04:00
initializing = false;
2016-04-03 19:52:13 +02:00
2019-06-05 09:46:59 +02:00
// Make a copy of all prototype properties, to prevent reference issues.
2021-07-02 17:24:29 +02:00
for (const p in prototype) {
2020-04-21 07:36:18 +02:00
prototype[p] = cloneObject(prototype[p]);
}
2016-04-05 14:35:11 -04:00
// Copy the properties over onto the new prototype
2021-07-02 17:24:29 +02:00
for (const name in prop) {
2016-04-05 14:35:11 -04:00
// Check if we're overwriting an existing function
2024-01-01 15:38:08 +01:00
prototype[name]
= typeof prop[name] === "function" && typeof _super[name] === "function" && fnTest.test(prop[name])
2020-05-11 22:22:32 +02:00
? (function (name, fn) {
2024-01-01 15:38:08 +01:00
return function () {
const tmp = this._super;
2020-05-11 22:22:32 +02:00
2024-08-12 22:52:43 +02:00
/*
* Add a new ._super() method that is the same method
* but on the super-class
*/
2024-01-01 15:38:08 +01:00
this._super = _super[name];
2020-05-11 22:22:32 +02:00
2024-08-12 22:52:43 +02:00
/*
* The method only need to be bound temporarily, so we
* remove it when we're done executing
*/
2024-01-01 15:38:08 +01:00
const ret = fn.apply(this, arguments);
this._super = tmp;
2020-05-11 22:22:32 +02:00
2024-01-01 15:38:08 +01:00
return ret;
};
}(name, prop[name]))
2020-05-11 22:22:32 +02:00
: prop[name];
2016-04-05 14:35:11 -04:00
}
2016-04-03 19:52:13 +02:00
/**
* The dummy class constructor
*/
2024-01-01 15:38:08 +01:00
function Class () {
2016-04-05 14:35:11 -04:00
// All construction is actually done in the init method
2016-05-03 19:09:38 -04:00
if (!initializing && this.init) {
this.init.apply(this, arguments);
}
2016-04-05 14:35:11 -04:00
}
2016-04-03 19:52:13 +02:00
2016-04-05 14:35:11 -04:00
// Populate our constructed prototype object
Class.prototype = prototype;
2016-04-03 19:52:13 +02:00
2016-04-05 14:35:11 -04:00
// Enforce the constructor to be what we expect
Class.prototype.constructor = Class;
2016-04-03 19:52:13 +02:00
2016-04-05 14:35:11 -04:00
// And make this class extendable
Class.extend = arguments.callee;
2016-04-03 19:52:13 +02:00
2016-04-05 14:35:11 -04:00
return Class;
};
2024-01-01 15:38:08 +01:00
}());
2016-03-30 13:44:16 +02:00
/**
* Define the clone method for later use. Helper Method.
2020-07-27 21:39:25 +02:00
* @param {object} obj Object to be cloned
* @returns {object} the cloned object
*/
2024-01-01 15:38:08 +01:00
function cloneObject (obj) {
if (obj === null || typeof obj !== "object") {
return obj;
}
2024-01-01 15:38:08 +01:00
if (obj.constructor.name === "RegExp") {
return new RegExp(obj);
}
2021-07-02 17:24:29 +02:00
const temp = obj.constructor(); // give temp the original obj's constructor
for (const key in obj) {
temp[key] = cloneObject(obj[key]);
2016-12-29 22:23:08 -03:00
if (key === "lockStrings") {
Log.log(key);
}
}
return temp;
}
2016-03-30 13:44:16 +02:00
/*************** DO NOT EDIT THE LINE BELOW ***************/
2017-07-25 21:36:30 -04:00
if (typeof module !== "undefined") {
module.exports = Class;
}