UNPKG

legacy-extends

Version:

Helper to extend a class including defining super_ and superConstruct (optional) on the derived class. Compatible with Node.js inherits and handles es6 classes using Reflect API

58 lines 2.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "default", { enumerable: true, get: function() { return _default; } }); function ensureProperties(names, self, args) { if (self[names[0]] !== args[0]) { var length = names.length; for(var index = 0; index < length; index++){ self[names[index]] = args[index]; } } } var hasProp = {}.hasOwnProperty; function extendLegacy(child_, parent_, options) { var child = child_; var parent = parent_; var initialize = options && options.ensureProperties && options.ensureProperties.length ? ensureProperties.bind(null, options.ensureProperties) : null; for(var key in parent){ if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() {} ctor.prototype = parent.prototype; child.prototype = new ctor(); child.prototype.constructor = child; var khild = child; khild.super_ = parent.prototype; khild.superConstruct = function construct() { // biome-ignore lint/complexity/noArguments: Apply arguments parent.prototype.constructor.apply(this, arguments); // biome-ignore lint/complexity/noArguments: Apply arguments !initialize || initialize(this, arguments); return this; }; } function extendReflect(child_, parent_, options) { var child = child_; var parent = parent_; var initialize = options && options.ensureProperties && options.ensureProperties.length ? ensureProperties.bind(null, options.ensureProperties) : null; Reflect.setPrototypeOf(child.prototype, parent.prototype); Reflect.setPrototypeOf(child, parent); var khild = child; khild.super_ = parent.prototype; khild.superConstruct = function construct() { // biome-ignore lint/complexity/noArguments: Apply arguments var self = Reflect.construct(parent, arguments, child); // biome-ignore lint/complexity/noArguments: Apply arguments !initialize || initialize(self, arguments); return self; }; } var _default = typeof Reflect === 'undefined' ? extendLegacy : extendReflect; /* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }