An easy to use prototypal inheritance model and generator.
An easy to use prototypal inheritance model.
$ npm install generate-js
Function Create method that gets called when creating a new object that inherits from Generation.proto.Generation A new Generation that inherits from Generation.Returns a new Generation that inherits from Generation.
Example:
var Generator = require'generate-js'; var Person = Generatorgenerate /* create method */ thisname = name || 'no-name'; thisage = age || 0; thissex = sex || 'unknown'; ;
Object An Object to be tested.Boolean true or false.Returns true if test object inherits from Generation, false otherwise.
Function An constructor to be generatorized.Generation A new Generation that who's create method is equivalent to calling new constructor();.Returns a new Generation that is equivalent to constructor.
NOTE: Some native constructors can NOT be generatorized.
Example:
// generatorize NodeJS' EventEmitter var Generator = require'generate-js' events = require'events'; var EventEmitter = GeneratortoGeneratoreventsEventEmitter; // EventEmitter.create() same as new events.EventEmitter(); // new generators can inherit all the abilities of EventEmitter like so. var MyNewGenerator = EventEmittergenerate /* create method */ {};
A new Generation that inherits from the Generation that generated it using the Generation.generate(create) method.
String The name of the create method.Name of this Generation.
Object Prototype inheritance for Creations created by this Generation.Generation.proto inherits from previous Generation's protos all the way up to Generation.proto which is equal to Creation.
Object Optional object descriptor that will be applied to all attaching properties.
Boolean States weather or not properties will be configurable, defaults to false.Boolean States weather or not properties will be enumerable, defaults to false.Boolean States weather or not properties will be writable, defaults to false.Object An object who's properties will be attached to this Generation's proto.Generation This Generation.Defines shared properties for all Creations created by this Generation.
Example:
/* * Defining prototype properties that can be overwritten by Creations using the `=` sign. */PersondefinePrototype writable: true console.log'Hello, my name is ' + thisname + '. What is yours?'; ; /* * Defining prototype properties that can NOT be overwritten by Creations using the `=` sign. */PersondefinePrototype writable: false console.log'Goodbye.'; ; /* * Defining prototype properties that use getters and setters. * NOTE: getter/setter prototype properties can NOT be overwritten by Creations using the `=` sign. */ var something = 'something'; PersondefinePrototype something: return something; something = newSomething; return something; ;;
Creation A new Creation that inherits from this Generation.proto.Creates a new Creation that inherits from this Generation.proto.
Example:
var jim = Personcreate'Jim' 10 'male'; jimname // 'Jim' jimage // 10 jimsex // 'male' jimsayHello; // prints out: 'Hello, my name is Jim. What is yours?' jimsayBye; // prints out: 'Goodbye.'
Function Create method that gets called when creating a new object that inherits from this Generation.proto.Generation A new Generation that inherits from this Generation.Returns a new Generation that inherits from this Generation.
Example:
var Student = Persongenerate /* create method */ // 'supercreate' method is only available in this create method scope. // NOTE: if the 'supercreate' method is not called implicitly it will be called with no arguments. thissupercreatename age sex; thisstudentId = studentId || 'A0000000000'; ; StudentdefinePrototype writable: true console.log'Sup? My student ID is: ' + thisstudentId + '.'; ; var sarah = Studentcreate'Sarah' 17 'female' 'A0123456789'; sarahname // 'Sarah' sarahage // 17 sarahsex // 'female' sarahstudentId // 'A0123456789' sarahsayHello; // prints out: 'Sup? My student ID is: A0123456789' sarahsayBye; // prints out: 'Goodbye.'
Object An Object to be tested.Boolean true or false.Returns true if test inherits from this Generation, false otherwise.
Object An Object to be tested.Boolean true or false.Returns true if test inherits from this Generation.proto, false otherwise.
A new Creation that inherits from a Generation's proto that created it using the Generation.create() method.
Object Optional object descriptor that will be applied to all attaching properties.
Boolean States weather or not properties will be configurable, defaults to false.Boolean States weather or not properties will be enumerable, defaults to false.Boolean States weather or not properties will be writable, defaults to false.Object An object who's properties will be attached to this Creation.Creation This Creation.Defines properties on this Creation.
Example:
/* * Jim is stubborn and blue will always be his favorite color. */jimdefineProperties writable: false favoriteColor: 'blue' ; /* * Sarah is indecisive and pink may not be her favorite color tomorrow. */sarahdefineProperties writable: true favoriteColor: 'pink' ;
Object Prototype of this Creation.Returns the prototype of this Creation.
Object Super prototype of this Creation.Returns the super prototype of this Creation.
Michaelangelo Jong
The MIT License (MIT)
Copyright (c) 2014-2015 Michaelangelo Jong
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.