1.0.12 • Published 10 months ago

legacy-extends v1.0.12

Weekly downloads
3
License
MIT
Repository
github
Last release
10 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

11 months ago

1.0.8

11 months ago

1.0.7

11 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.11

10 months ago

1.0.10

10 months ago

1.0.12

10 months ago

1.0.1

3 years ago

1.0.0

5 years ago

0.4.0

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago