<commit_before># trope.js
<a id="overview"></a>
## Overview
Trope is a simple interface for JavaScript inheritance that provides some extra, useful capabilities. At the core of this tool are prototypes which are used to maintain [private state](https://github.com/ogupte/trope#private-properties), allow access to [super functions](https://github.com/ogupte/trope#inheritance), and create objects with [multiple inheritance](https://github.com/ogupte/trope#multiple-inheritance). It's easy to start integrating with most projects since [native JS](https://github.com/ogupte/trope#native-js-compatibility) constructors, object prototypes and ES6 classes are already valid Trope definitions.

### [API Documentation](https://github.com/ogupte/trope/blob/master/trope-api.md)

<a id="features"></a>
## Features
* [Simplified object inheritance](https://github.com/ogupte/trope#inheritance)
* [Private object state](https://github.com/ogupte/trope#description-private-members)
* [Super functions](https://github.com/ogupte/trope#description-super)
* [Multiple inheritance](https://github.com/ogupte/trope#description-multiple-inheritance)
* [Compatible with native JS](https://github.com/ogupte/trope#native-js-compatibility)

<a id="examples"></a>
### Examples
* [Object factories](https://github.com/ogupte/trope#object-factories)
* [Private properties](https://github.com/ogupte/trope#private-properties)
* [Inheritance](https://github.com/ogupte/trope#inheritance)
* [Multiple Inheritance](https://github.com/ogupte/trope#multiple-inheritance)
* [Native JS Compatibility](https://github.com/ogupte/trope#native-js-compatibility)

<a id="object-factories"></a>
#### Object factories
A simple object factory.
```javascript
var Greeter = Trope({
    setName: function (name) {
        this.name = name;
    },
    sayHello: function () {
        return 'Hello, ' + this.name + '!';
    }
});

var greeter = Greeter.create();
greeter.setName('Bertrand');
greeter.sayHello(); // 'Hello, Bertrand!'
```
Pass in an *initializer* function, to set state upon creation.
```javascript
var Greeter = Trope(function (name) {
    this.setName(name);
},{
    setName: function (name) {
        this.name = name;
    },
    sayHello: function () {
        return 'Hello, ' + this.name + '!';
    }
});

var greeter = Greeter.create('Bertrand');
greeter.name; // 'Bertrand'
greeter.sayHello(); // 'Hello, Bertrand!'
```
New objects can also be created by calling the Trope as a function with or without the `new` operator.
```javascript
greeter = Greeter.create('Bertrand');
greeter = Greeter('Bertrand');
greeter = new Greeter('Bertrand');
```

<a id="private-properties"></a>
#### Private properties
`privacy` mode can be enabled to prevent object properties from being accessed by an outside context.
```javascript
var Greeter = Trope({ privacy: true }, function (name) {
    this.setName(name);
},{
    setName: function (name) {
        this.name = name;
    },
    sayHello: function () {
        return 'Hello, ' + this.name + '!';
    }
});

var greeter = Greeter.create('Bertrand');
greeter.name; // undefined
greeter.sayHello(); // 'Hello, Bertrand!'
```
Public properties can still be set in `privacy` mode by setting them on `this.exports`.
```javascript
var Greeter = Trope({ privacy: true }, function (name) {
    this.setName(name);
},{
    setName: function (name) {
        this.name = name;
        this.exports.exposedName = name;
    },
    sayHello: function () {
        return 'Hello, ' + this.name + '!';
    }
});

var greeter = Greeter.create('Bertrand');
greeter.name; // undefined
greeter.exposedName; // 'Bertrand'
greeter.sayHello(); // 'Hello, Bertrand!'
```
See how [private properties](https://github.com/ogupte/trope#description-private-members) are implemented in Trope.

<a id="inheritance"></a>
#### Inheritance
Start with an existing definition
```javascript
var Animal = Trope({
    getLongName: function () {
        return 'Animalia';
    }
});
```
Inherit from base definitions by chaining off of them with `turn`, or `proto`, or `extend` and passing in a new definition. Functions can be overloaded and the original can be called with `this.super()`.
```javascript
var Vertebrate = Animal.turn({
    getLongName: function () {
        return this.super() + ' Chordata';
    }
});

var Mammal = Vertebrate.turn({
    getLongName: function () {
        return this.super() + ' Mammalia';
    }
});

var mammal = Mammal.create();
mammal.getLongName(); // 'Animalia Chordata Mammalia'
```
The native `instanceof` operator works as expected since Trope inheritance is based on JavaScript's native inheritance mechanism.
```javascript
mammal instanceof Mammal;
mammal instanceof Vertebrate;
mammal instanceof Animal;
```

See how [this.super](https://github.com/ogupte/trope#description-super) is implemented in Trope.

<a id="multiple-inheritance"></a>
##### Multiple Inheritance
First we define two unrelated Tropes `Logger` and `EventEmitter`.
```javascript
var Logger = Trope({
    log: function (msg) {
        console.log(msg);
    }
});

var EventEmitter = Trope({
    on: function (name, handler) {
        if (!this.eventMap) {
            this.eventMap = {};
        }
        if (!this.eventMap[name]) {
            this.eventMap[name] = [];
        }
        this.eventMap[name].push(handler);
    },
    fire: function (name, data) {
        if (this.eventMap && this.eventMap[name]) {
            this.eventMap[name].forEach(function (handler) {
                handler.call(this, data);
            }.bind(this));
        }
    }
});
```
They can be combined to create a new Trope `Eventedlogger`.
```javascript
var EventedLogger = Trope([Logger, EventEmitter]);

var eventedLogger = EventedLogger.create();
eventedLogger.on('logme', function (msg) {
	eventedLogger.log('LOGME: ' + msg);
});
eventedLogger.fire('logme', 'hello'); // logs 'LOGME: hello'
eventedLogger.fire('logme', 'world'); // logs 'LOGME: world'
```
Multiple inheritance can often result in some interesting combinations.

For example, define a simple `Cat`.
```javascript
// Define a `Cat`
var Cat = Trope(function (name) { // init function
    this.name = name;
}, {
    vocalize: function () {
        return 'Meow!';
    }
});

var cat = Cat.create('Raja');
cat.vocalize(); // 'Meow!'
```
<a id="LoggingEventedCat"></a>
Now inherit from `Logger`, `EventEmitter`, and `Cat` while passing in a new definitiong to create something completely different.
```javascript
var LoggingEventedCat = Trope([Logger, EventEmitter, Cat], function (name) { // init function
		this.super.as(Cat)(name);
		this.on('meow', function (sound) {
			console.log(this.name + ': ' + sound);
		});
	},{ // overload the `vocalize` method inherited from `Cat`
		vocalize: function (sound) {
			sound = sound || this.super.as(Cat)();
			this.fire('meow', sound);
		}
	});

var loggingEventedCat = LoggingEventedCat.create('Raja');
loggingEventedCat.vocalize(); // logs 'Raja: Meow!'
loggingEventedCat.vocalize('Purr...'); // logs 'Raja: Purr...'
```
Yes, defining a `LoggingEventedCat` is entirely possible in Trope, however the usefulness of these composites is at the discretion of the developer.

See how [multiple inheritance](https://github.com/ogupte/trope#description-multiple-inheritance) and [this.super](https://github.com/ogupte/trope#description-super) are implemented in Trope.

<a id="native-js-compatibility"></a>
#### Native JS Compatibility
Trope is completely compatibile with native JS because JavaScript constructors are valid Trope definitions! This makes it easy to start using it without having to modify an existing code base.
```javascript<commit_msg>fixed spelling mistake in readme<commit_after># trope.js
<a id="overview"></a>
## Overview
Trope is a simple interface for JavaScript inheritance that provides some extra, useful capabilities. At the core of this tool are prototypes which are used to maintain [private state](https://github.com/ogupte/trope#private-properties), allow access to [super functions](https://github.com/ogupte/trope#inheritance), and create objects with [multiple inheritance](https://github.com/ogupte/trope#multiple-inheritance). It's easy to start integrating with most projects since [native JS](https://github.com/ogupte/trope#native-js-compatibility) constructors, object prototypes and ES6 classes are already valid Trope definitions.

### [API Documentation](https://github.com/ogupte/trope/blob/master/trope-api.md)

<a id="features"></a>
## Features
* [Simplified object inheritance](https://github.com/ogupte/trope#inheritance)
* [Private object state](https://github.com/ogupte/trope#description-private-members)
* [Super functions](https://github.com/ogupte/trope#description-super)
* [Multiple inheritance](https://github.com/ogupte/trope#description-multiple-inheritance)
* [Compatible with native JS](https://github.com/ogupte/trope#native-js-compatibility)

<a id="examples"></a>
### Examples
* [Object factories](https://github.com/ogupte/trope#object-factories)
* [Private properties](https://github.com/ogupte/trope#private-properties)
* [Inheritance](https://github.com/ogupte/trope#inheritance)
* [Multiple Inheritance](https://github.com/ogupte/trope#multiple-inheritance)
* [Native JS Compatibility](https://github.com/ogupte/trope#native-js-compatibility)

<a id="object-factories"></a>
#### Object factories
A simple object factory.
```javascript
var Greeter = Trope({
    setName: function (name) {
        this.name = name;
    },
    sayHello: function () {
        return 'Hello, ' + this.name + '!';
    }
});

var greeter = Greeter.create();
greeter.setName('Bertrand');
greeter.sayHello(); // 'Hello, Bertrand!'
```
Pass in an *initializer* function, to set state upon creation.
```javascript
var Greeter = Trope(function (name) {
    this.setName(name);
},{
    setName: function (name) {
        this.name = name;
    },
    sayHello: function () {
        return 'Hello, ' + this.name + '!';
    }
});

var greeter = Greeter.create('Bertrand');
greeter.name; // 'Bertrand'
greeter.sayHello(); // 'Hello, Bertrand!'
```
New objects can also be created by calling the Trope as a function with or without the `new` operator.
```javascript
greeter = Greeter.create('Bertrand');
greeter = Greeter('Bertrand');
greeter = new Greeter('Bertrand');
```

<a id="private-properties"></a>
#### Private properties
`privacy` mode can be enabled to prevent object properties from being accessed by an outside context.
```javascript
var Greeter = Trope({ privacy: true }, function (name) {
    this.setName(name);
},{
    setName: function (name) {
        this.name = name;
    },
    sayHello: function () {
        return 'Hello, ' + this.name + '!';
    }
});

var greeter = Greeter.create('Bertrand');
greeter.name; // undefined
greeter.sayHello(); // 'Hello, Bertrand!'
```
Public properties can still be set in `privacy` mode by setting them on `this.exports`.
```javascript
var Greeter = Trope({ privacy: true }, function (name) {
    this.setName(name);
},{
    setName: function (name) {
        this.name = name;
        this.exports.exposedName = name;
    },
    sayHello: function () {
        return 'Hello, ' + this.name + '!';
    }
});

var greeter = Greeter.create('Bertrand');
greeter.name; // undefined
greeter.exposedName; // 'Bertrand'
greeter.sayHello(); // 'Hello, Bertrand!'
```
See how [private properties](https://github.com/ogupte/trope#description-private-members) are implemented in Trope.

<a id="inheritance"></a>
#### Inheritance
Start with an existing definition
```javascript
var Animal = Trope({
    getLongName: function () {
        return 'Animalia';
    }
});
```
Inherit from base definitions by chaining off of them with `turn`, or `proto`, or `extend` and passing in a new definition. Functions can be overloaded and the original can be called with `this.super()`.
```javascript
var Vertebrate = Animal.turn({
    getLongName: function () {
        return this.super() + ' Chordata';
    }
});

var Mammal = Vertebrate.turn({
    getLongName: function () {
        return this.super() + ' Mammalia';
    }
});

var mammal = Mammal.create();
mammal.getLongName(); // 'Animalia Chordata Mammalia'
```
The native `instanceof` operator works as expected since Trope inheritance is based on JavaScript's native inheritance mechanism.
```javascript
mammal instanceof Mammal;
mammal instanceof Vertebrate;
mammal instanceof Animal;
```

See how [this.super](https://github.com/ogupte/trope#description-super) is implemented in Trope.

<a id="multiple-inheritance"></a>
##### Multiple Inheritance
First we define two unrelated Tropes `Logger` and `EventEmitter`.
```javascript
var Logger = Trope({
    log: function (msg) {
        console.log(msg);
    }
});

var EventEmitter = Trope({
    on: function (name, handler) {
        if (!this.eventMap) {
            this.eventMap = {};
        }
        if (!this.eventMap[name]) {
            this.eventMap[name] = [];
        }
        this.eventMap[name].push(handler);
    },
    fire: function (name, data) {
        if (this.eventMap && this.eventMap[name]) {
            this.eventMap[name].forEach(function (handler) {
                handler.call(this, data);
            }.bind(this));
        }
    }
});
```
They can be combined to create a new Trope `Eventedlogger`.
```javascript
var EventedLogger = Trope([Logger, EventEmitter]);

var eventedLogger = EventedLogger.create();
eventedLogger.on('logme', function (msg) {
	eventedLogger.log('LOGME: ' + msg);
});
eventedLogger.fire('logme', 'hello'); // logs 'LOGME: hello'
eventedLogger.fire('logme', 'world'); // logs 'LOGME: world'
```
Multiple inheritance can often result in some interesting combinations.

For example, define a simple `Cat`.
```javascript
// Define a `Cat`
var Cat = Trope(function (name) { // init function
    this.name = name;
}, {
    vocalize: function () {
        return 'Meow!';
    }
});

var cat = Cat.create('Raja');
cat.vocalize(); // 'Meow!'
```
<a id="LoggingEventedCat"></a>
Now inherit from `Logger`, `EventEmitter`, and `Cat` while passing in a new definition to create something completely different.
```javascript
var LoggingEventedCat = Trope([Logger, EventEmitter, Cat], function (name) { // init function
		this.super.as(Cat)(name);
		this.on('meow', function (sound) {
			console.log(this.name + ': ' + sound);
		});
	},{ // overload the `vocalize` method inherited from `Cat`
		vocalize: function (sound) {
			sound = sound || this.super.as(Cat)();
			this.fire('meow', sound);
		}
	});

var loggingEventedCat = LoggingEventedCat.create('Raja');
loggingEventedCat.vocalize(); // logs 'Raja: Meow!'
loggingEventedCat.vocalize('Purr...'); // logs 'Raja: Purr...'
```
Yes, defining a `LoggingEventedCat` is entirely possible in Trope, however the usefulness of these composites is at the discretion of the developer.

See how [multiple inheritance](https://github.com/ogupte/trope#description-multiple-inheritance) and [this.super](https://github.com/ogupte/trope#description-super) are implemented in Trope.

<a id="native-js-compatibility"></a>
#### Native JS Compatibility
Trope is completely compatibile with native JS because JavaScript constructors are valid Trope definitions! This makes it easy to start using it without having to modify an existing code base.
```javascript
{'fraction_non_alphanumeric': 0.15100495954058993, 'fraction_numerical': 0.00013051422605063953, 'mean_word_length': 4.076184166942697, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 38, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5463893', 'n_tokens_mistral': 4828, 'n_tokens_neox': 4616, 'n_words': 1287}