Skip to content

Commit 23395bb

Browse files
author
Michaelangelo Jong
committed
Generator v3.0.0
- API changes (not compatible v2.*.* or lower). - Heavily optimized 50x performance on creating new instances.
1 parent 0f4315b commit 23395bb

4 files changed

Lines changed: 200 additions & 601 deletions

File tree

README.md

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
# Release v2.1.6
1+
# Release v3.0.0
22

3-
# [v3.0.0](new-api.md) has *BREAKING* changes and is not compatible for inheirtance with *ANY* v2.\*.\* Generators.
3+
# v3.0.0 has *BREAKING* changes and is not compatible for inheirtance with *ANY* v2.\*.\* Generators.
44

55
## Table of Contents
66

77
* [ Generator ](#generator)
88
* [ Generator.generate(create) ](#generate)
99
* [ Generator.isGenerator(test) ](#is-generator)
10-
* [ Generator.toGenerator(constructor) ](#to-generator)
10+
* [ Generator.toGenerator(constructor, create) ](#to-generator)
1111
* [ Class: Generation ](#class-generation)
12-
* [ Generation.name ](#generation-name)
13-
* [ Generation.proto ](#generation-proto)
12+
* [ new Generation() ](#generation-create)
1413
* [ Generation.definePrototype([descriptor,] properties) ](#generation-define-prototype)
15-
* [ Generation.create() ](#generation-create)
1614
* [ Generation.generate(create) ](#generation-generate)
1715
* [ Generation.isCreation(test) ](#generation-is-creation)
1816
* [ Generation.isGeneration(test) ](#generation-is-generation)
@@ -35,8 +33,8 @@ $ npm install generate-js
3533
<a name="generate"></a>
3634
## Generator.generate(create)
3735

38-
* *create* `Function` Create method that gets called when creating a new object that inherits from [Generation.proto](#generation-proto).
39-
* *return*: `Generation` A new [Generation](#class-generation) that inherits from [Generation](#class-generation).
36+
* *create* `Function` Constructor that with inherit form [Generation](#class-generation).
37+
* *return*: `Constructor` The inputed `create` function.
4038

4139
Returns a new [Generation](#class-generation) that inherits from [Generation](#class-generation).
4240

@@ -63,12 +61,13 @@ var Person = Generator.generate(
6361
Returns `true` if *test* object inherits from [Generation](#class-generation), `false` otherwise.
6462

6563
<a name="to-generator"></a>
66-
## Generator.toGenerator(constructor)
64+
## Generator.toGenerator(constructor, create)
6765

6866
* *constructor* `Function` An constructor to be generatorized.
69-
* *return*: `Generation` A new [Generation](#class-generation) that who's create method is equivalent to calling `new constructor();`.
67+
* *create* `Function` Constructor that with inherit form [Generation](#class-generation) and *constructor*.
68+
* *return*: `Constructor` The inputed `create` function.
7069

71-
Returns a new [Generation](#class-generation) that is equivalent to *constructor*.
70+
Returns a new [Generation](#class-generation) that inherits from [Generation](#class-generation) and *constructor*.
7271

7372
*NOTE*: Some native constructors can *NOT* be generatorized.
7473

@@ -78,14 +77,20 @@ Example:
7877
var Generator = require('generate-js'),
7978
events = require('events');
8079

81-
var EventEmitter = Generator.toGenerator(events.EventEmitter);
80+
var EventEmitter = Generator.toGenerator(events.EventEmitter, function EventEmitter() {
81+
events.EventEmitter.call(this); //call the super constructor
82+
});
8283

83-
// EventEmitter.create() same as new events.EventEmitter();
84+
// new EventEmitter() same as new events.EventEmitter();
8485

8586
// new generators can inherit all the abilities of EventEmitter like so.
8687
var MyNewGenerator = EventEmitter.generate(
8788
/* create method */
88-
function MyNewGenerator() {}
89+
function MyNewGenerator() {
90+
EventEmitter.call(this); //call the super constructor
91+
92+
// your code
93+
}
8994
);
9095

9196
```
@@ -95,17 +100,25 @@ var MyNewGenerator = EventEmitter.generate(
95100

96101
A new Generation that inherits from the Generation that generated it using the [Generation.generate(create)](#generation-generate) method.
97102

98-
<a name="generation-name"></a>
99-
## Generation.name
100-
* *name* `String` The name of the create method.
103+
<a name="generation-create"></a>
104+
## new Generation()
105+
106+
* *return*: `Creation` A new [Creation](#class-creation) that is an instance of *this* [Generation](#generation).
101107

102-
Name of *this* [Generation](#class-generation).
108+
Creates a new [Creation](#class-creation) that is an instance of *this* [Generation](#generation).
103109

104-
<a name="generation-proto"></a>
105-
## Generation.proto
106-
* *proto* `Object` Prototype inheritance for [Creations](#class-creation) created by *this* [Generation](#class-generation).
110+
Example:
111+
```javascript
112+
var jim = new Person('Jim', 10, 'male');
107113

108-
Generation.proto inherits from previous Generation's protos all the way up to Generation.proto which is equal to [Creation](class-creation).
114+
jim.name // 'Jim'
115+
jim.age // 10
116+
jim.sex // 'male'
117+
118+
jim.sayHello(); // prints out: 'Hello, my name is Jim. What is yours?'
119+
jim.sayBye(); // prints out: 'Goodbye.'
120+
121+
```
109122

110123
<a name="generation-define-prototype"></a>
111124
## Generation.definePrototype([descriptor,] properties)
@@ -114,7 +127,7 @@ Generation.proto inherits from previous Generation's protos all the way up to Ge
114127
* *configurable* `Boolean` States weather or not properties will be *configurable*, defaults to `false`.
115128
* *enumerable* `Boolean` States weather or not properties will be *enumerable*, defaults to `false`.
116129
* *writable* `Boolean` States weather or not properties will be *writable*, defaults to `false`.
117-
* *properties* `Object` An object who's properties will be attached to *this* [Generation's proto](#generation-proto).
130+
* *properties* `Object` An object who's properties will be attached to *this* [Generation.prototype](#generation).
118131
* *return*: `Generation` *This* [Generation](#class-generation).
119132

120133
Defines shared properties for all [Creations](#class-creation) created by *this* [Generation](#class-generation).
@@ -171,31 +184,11 @@ Person.definePrototype(
171184

172185
```
173186

174-
<a name="generation-create"></a>
175-
## Generation.create()
176-
177-
* *return*: `Creation` A new [Creation](#class-creation) that inherits from *this* [Generation.proto](#generation-proto).
178-
179-
Creates a new [Creation](#class-creation) that inherits from *this* [Generation.proto](#generation-proto).
180-
181-
Example:
182-
```javascript
183-
var jim = Person.create('Jim', 10, 'male');
184-
185-
jim.name // 'Jim'
186-
jim.age // 10
187-
jim.sex // 'male'
188-
189-
jim.sayHello(); // prints out: 'Hello, my name is Jim. What is yours?'
190-
jim.sayBye(); // prints out: 'Goodbye.'
191-
192-
```
193-
194187
<a name="generation-generate"></a>
195188
## Generation.generate(create)
196189

197-
* *create* `Function` Create method that gets called when creating a new object that inherits from *this* [Generation.proto](#generation-proto).
198-
* *return*: `Generation` A new [Generation](#class-generation) that inherits from *this* [Generation](#class-generation).
190+
* *create* `Function` Constructor that with inherit form [Generation](#class-generation).
191+
* *return*: `Constructor` The inputed `create` function.
199192

200193
Returns a new [Generation](#class-generation) that inherits from *this* [Generation](#class-generation).
201194

@@ -204,9 +197,7 @@ Example:
204197
var Student = Person.generate(
205198
/* create method */
206199
function Student(name, age, sex, studentId) {
207-
// 'supercreate' method is only available in this create method scope.
208-
// NOTE: if the 'supercreate' method is not called implicitly it will be called with no arguments.
209-
this.supercreate(name, age, sex);
200+
Person.call(this, name, age, sex);
210201
this.studentId = studentId || 'A0000000000';
211202
}
212203
);
@@ -222,7 +213,7 @@ Student.definePrototype(
222213
}
223214
);
224215

225-
var sarah = Student.create('Sarah', 17, 'female', 'A0123456789');
216+
var sarah = new Student('Sarah', 17, 'female', 'A0123456789');
226217

227218
sarah.name // 'Sarah'
228219
sarah.age // 17
@@ -248,12 +239,12 @@ Returns `true` if *test* inherits from *this* [Generation](#class-generation), `
248239
* *test* `Object` An Object to be tested.
249240
* *return*: `Boolean` `true` or `false`.
250241

251-
Returns `true` if *test* inherits from *this* [Generation.proto](#generation-proto), `false` otherwise.
242+
Returns `true` if *test* is an instance of *this* [Generation](#generation), `false` otherwise.
252243

253244
<a name="class-creation"></a>
254245
## Class: Creation
255246

256-
A new Creation that inherits from a [Generation's proto](#generation-proto) that created it using the [Generation.create()](#generation-create) method.
247+
A instance [Generation](#generation) that created it using the [new Generation()](#generation-create).
257248

258249
<a name="creation-define-properties"></a>
259250
## Creation.defineProperties([descriptor,] properties)

0 commit comments

Comments
 (0)