Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
generate-js
Advanced tools
An easy to use prototypal inheritance model.
$ npm install generate-js
Function
Constructor that with inherit form Generation.Constructor
The inputed create
function.Returns a new Generation that inherits from Generation.
Example:
var Generator = require('generate-js');
var Person = Generator.generate(
/* create method */
function Person(name, age, sex) {
this.name = name || 'no-name';
this.age = age || 0;
this.sex = 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.Function
Constructor that with inherit form Generation and constructor.Constructor
The inputed create
function.Returns a new Generation that inherits from Generation and constructor.
NOTE: Some native constructors can NOT be generatorized.
Example:
// generatorize NodeJS' EventEmitter
var Generator = require('generate-js'),
events = require('events');
var EventEmitter = Generator.toGenerator(events.EventEmitter, function EventEmitter() {
events.EventEmitter.call(this); //call the super constructor
});
// new EventEmitter() same as new events.EventEmitter();
// new generators can inherit all the abilities of EventEmitter like so.
var MyNewGenerator = EventEmitter.generate(
/* create method */
function MyNewGenerator() {
EventEmitter.call(this); //call the super constructor
// your code
}
);
A new Generation that inherits from the Generation that generated it using the Generation.generate(create) method.
Creation
A new Creation that is an instance of this Generation.Creates a new Creation that is an instance of this Generation.
Example:
var jim = new Person('Jim', 10, 'male');
jim.name // 'Jim'
jim.age // 10
jim.sex // 'male'
jim.sayHello(); // prints out: 'Hello, my name is Jim. What is yours?'
jim.sayBye(); // prints out: 'Goodbye.'
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.prototype.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.
*/
Person.definePrototype(
{
writable: true
},
{
sayHello: function() {
console.log('Hello, my name is ' + this.name + '. What is yours?');
}
}
);
/*
* Defining prototype properties that can NOT be overwritten by Creations using the `=` sign.
*/
Person.definePrototype(
{
writable: false
},
{
sayBye: function() {
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.
*/
(function(){
var something = 'something';
Person.definePrototype({
something: {
get: function() {
return something;
},
set: function (newSomething) {
something = newSomething;
return something;
}
}
});
}());
Function
Constructor that with inherit form Generation.Constructor
The inputed create
function.Returns a new Generation that inherits from this Generation.
Example:
var Student = Person.generate(
/* create method */
function Student(name, age, sex, studentId) {
Person.call(this, name, age, sex);
this.studentId = studentId || 'A0000000000';
}
);
Student.definePrototype(
{
writable: true
},
{
sayHello: function () {
console.log('Sup? My student ID is: ' + this.studentId + '.');
}
}
);
var sarah = new Student('Sarah', 17, 'female', 'A0123456789');
sarah.name // 'Sarah'
sarah.age // 17
sarah.sex // 'female'
sarah.studentId // 'A0123456789'
sarah.sayHello(); // prints out: 'Sup? My student ID is: A0123456789'
sarah.sayBye(); // 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 is an instance of this Generation, false
otherwise.
A instance Generation that created it using the new Generation().
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.
*/
jim.defineProperties(
{
writable: false
},
{
favoriteColor: 'blue'
}
);
/*
* Sarah is indecisive and pink may not be her favorite color tomorrow.
*/
sarah.defineProperties(
{
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-2016 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.
FAQs
An easy to use prototypal inheritance model and generator.
The npm package generate-js receives a total of 3,308 weekly downloads. As such, generate-js popularity was classified as popular.
We found that generate-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.