# msff-constructor-overwrite

> Overwrite a constructor function in JavaScript

Latest version **1.2.0** (published 2015-08-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install msff-constructor-overwrite
pnpm add msff-constructor-overwrite
yarn add msff-constructor-overwrite
bun add msff-constructor-overwrite
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2015-08-10 |
| First published | 2015-08-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Marcos SF Filho |
| Maintainers | marcossffilho |
| Keywords | class, constructor, extend |

## Links

- npm: https://www.npmjs.com/package/msff-constructor-overwrite
- Repository: https://github.com/marcossffilho/msff-constructor-overwrite
- Homepage: https://github.com/marcossffilho/msff-constructor-overwrite#readme
- Issues: https://github.com/marcossffilho/msff-constructor-overwrite/issues
- npm.io page: https://npm.io/package/msff-constructor-overwrite

## Recent versions

- 1.2.0 (latest) — 2015-08-10
- 1.1.0 — 2015-08-06
- 1.0.0 — 2015-08-02

## README

# Constructor Overwrite

Overwrite a constructor function in JavaScript.

## Project Status

Version 1.2.0 released in August 10, 2015 ([CHANGELOG](CHANGELOG.md)).

Travis CI:
- **New features**: [![Build Status][travis-ci-develop]][travis-ci-project]
- **Stable version**: [![Build Status][travis-ci-master]][travis-ci-project]

## What this project is for?

This project is recommended to extend or rewrite constructor functions in
javascript using a lightweight code.

## What this project is not for?

This project is **not** recommended to give class inheritance feature to
ES5/ES3 projects. In this case, we recommend to use

- ES2015 code transpiled with tools like [Babel][babel]
- Languages with class support that compile to JavaScript, like
  [CoffeeScript][coffeescript] and [TypeScript][typescript], or one of
  [these][langs-to-js].

## Install

This project is available as npm package:

```
npm install --save msff-constructor-overwrite
```

If you're using TypeScript, you must also link the definitions files
with [TSD][tsd]:

```
tsd link
```

## Usage

```javascript
var overwrite = require('msff-constructor-overwrite');
// overwrite(oldConstructor, newConstructor);
// overwrite(oldConstructor, newConstructor, propertyForOldConstructor);
```

The `newConstructor` will receive the prototype of `oldConstructor`. If
`newConstructor` already have a prototype, it'll be overwritten.

In addition, `newConstructor` will have the old constructor stored in
a static property named `___oldConstructor`. You can use a custom name by
using the third argument.

### Overwriting constructor

```javascript
var overwrite = require('msff-constructor-overwrite');

var Person = (function() {
  function Person(name) {
    this.name = name;
  }

  return Person;
})()

Person = overwrite(Person, function(firstName, lastName) {
  this.name = [firstName, lastName].join(" ");
});

var me = new Person('Marcos', 'Filho')
// me.name => Marcos Filho
```

### Extending constructor

```javascript
var overwrite = require('msff-constructor-overwrite');

var Person = (function() {
  function Person(name) {
    this.name = name;
  }

  return Person;
})()

Person = overwrite(
  Person,
  // You must name the constructor to extend
  function Person_Extended(firstName, lastName) {
    this.fullName = [firstName, lastName].join(" ");

    // Calling the old constructor
    Person_Extended.old.apply(this, arguments)
  },
  // By default the old goes on `___oldConstructor`
  // Use the third parameter to overwrite the property
  "old"
);

var me = new Person('Marcos', 'Filho')
// me.name => Marcos
// me.fullName => Marcos Filho
```



[travis-ci-project]: https://travis-ci.org/marcossffilho/msff-ng-decorators
[travis-ci-develop]: https://travis-ci.org/marcossffilho/msff-constructor-overwrite.svg?branch=develop
[travis-ci-master]:  https://travis-ci.org/marcossffilho/msff-constructor-overwrite.svg?branch=master
[babel]:             https://babeljs.io/
[coffeescript]:      http://coffeescript.org/
[typescript]:        http://typescriptlang.org/
[langs-to-js]:       https://github.com/jashkenas/coffeescript/wiki/list-of-languages-that-compile-to-JS
[tsd]:               http://definitelytyped.org/tsd/

---
_Source: https://npm.io/package/msff-constructor-overwrite · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
