1.0.12 • Published 5 months ago

legacy-extends v1.0.12

Weekly downloads
3
License
MIT
Repository
github
Last release
5 months ago

legacy-extends

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.

es6

function Animal(name) {
  this._name = name || 'No name';
}

Animal.prototype.name = function name() {
  return this._name;
};

Animal.prototype.move = function move() {
  return this.name() + ' unknown';
};

function Dog(name) {
  return Dog.superConstruct.call(this, name);
}
extend(Dog, Animal);

Dog.prototype.name = function name() {
  return 'Dog ' + Dog.super_.name.call(this);
};

Dog.prototype.move = function move() {
  return this.name() + ' run';
};

var animal = new Animal();
assert.equal(animal.move(), 'No name unknown');

var dog = new Dog('Rover');
assert.equal(dog.move(), 'Dog Rover run');

es5

class Animal {
  constructor(name) {
    this._name = name || 'No name';
  }

  name() {
    return this._name;
  }

  move() {
    return this.name() + ' unknown';
  }
}

class Dog {
  constructor(name) {
    return Dog.superConstruct.call(this, name);
  }

  name() {
    return 'Dog ' + Dog.super_.name.call(this);
  }

  move() {
    return this.name() + ' run';
  }
}
extend(Dog, Animal);

var animal = new Animal();
assert.equal(animal.move(), 'No name unknown');

var dog = new Dog('Rover');
assert.equal(dog.move(), 'Dog Rover run');
1.0.9

5 months ago

1.0.8

5 months ago

1.0.7

5 months ago

1.0.6

5 months ago

1.0.5

5 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.11

5 months ago

1.0.10

5 months ago

1.0.12

5 months ago

1.0.1

3 years ago

1.0.0

5 years ago

0.4.0

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago