Skip to content

Instantly share code, notes, and snippets.

@porqz
Created May 11, 2017 09:54
Show Gist options
  • Save porqz/0666d130d8da41a2fe2039025cabb1de to your computer and use it in GitHub Desktop.
Save porqz/0666d130d8da41a2fe2039025cabb1de to your computer and use it in GitHub Desktop.
Simple JavaScript inheritance implementation (my old code)
(function () {
var defaults = {
defaultsPropertyName: "defaults"
};
/**
* Реализует простое наследование
*
* @param {Function} constructor Функция-конструктор класса-родителя
* @param {?Object} prototype Объект, который будет использован в качестве прототипа для потомка
*
* @return {Object} Функция-конструктор класс-потомка
*/
Function.prototype.inheritFrom = Function.prototype.inheritFrom || function (constructor, prototype) {
var intermediateFunction = function () {};
intermediateFunction.prototype = constructor.prototype;
this.prototype = new intermediateFunction();
this.prototype.constructor = this;
if (typeof prototype != "undefined") {
for (var propertyName in prototype) {
this.prototype[propertyName] = prototype[propertyName];
if (propertyName == defaults.defaultsPropertyName) {
ObjectUtils.extend(this.prototype[propertyName], constructor.prototype[propertyName], function (propertyName, propertyValue, newPropertyValue) {
return propertyValue;
});
}
}
}
this.superprototype = constructor.prototype;
return this;
};
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment