npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

generate-js

v3.1.2

Published

An easy to use prototypal inheritance model and generator.

Downloads

24,115

Readme

Release v3.1.2

v3.0.0 has BREAKING changes and is not compatible for inheirtance with ANY v2.*.* Generators.

Table of Contents

Generator

An easy to use prototypal inheritance model.

Install:

$ npm install generate-js

Generator.generate(create)

  • create Function Constructor that with inherit form Generation.
  • return: 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';
	}
);

Generator.isGenerator(test)

  • test Object An Object to be tested.
  • return: Boolean true or false.

Returns true if test object inherits from Generation, false otherwise.

Generator.generateFrom(constructor, create)

  • constructor Function An constructor to be generatorized.
  • create Function Constructor that with inherit form Generation and constructor.
  • return: 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.generateFrom(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
  }
);

Class: Generation

A new Generation that inherits from the Generation that generated it using the Generation.generate(create) method.

new 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.'

Generation.definePrototype([descriptor,] properties)

  • descriptor Object Optional object descriptor that will be applied to all attaching properties.
    • configurable Boolean States weather or not properties will be configurable, defaults to false.
    • enumerable Boolean States weather or not properties will be enumerable, defaults to false.
    • writable Boolean States weather or not properties will be writable, defaults to false.
  • properties Object An object who's properties will be attached to this Generation.prototype.
  • return: 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;
			}
		}
	});
}());

Generation.generate(create)

  • create Function Constructor that with inherit form Generation.
  • return: 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.'

Generation.isGeneration(test)

  • test Object An Object to be tested.
  • return: Boolean true or false.

Returns true if test inherits from this Generation, false otherwise.

Generation.isCreation(test)

  • test Object An Object to be tested.
  • return: Boolean true or false.

Returns true if test is an instance of this Generation, false otherwise.

Class: Creation

A instance Generation that created it using the new Generation().

Creation.defineProperties([descriptor,] properties)

  • descriptor Object Optional object descriptor that will be applied to all attaching properties.
    • configurable Boolean States weather or not properties will be configurable, defaults to false.
    • enumerable Boolean States weather or not properties will be enumerable, defaults to false.
    • writable Boolean States weather or not properties will be writable, defaults to false.
  • properties Object An object who's properties will be attached to this Creation.
  • return: 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'
	}
);

Creation.getProto()

  • return: Object Prototype of this Creation.

Returns the prototype of this Creation.

Creation.getSuper()

  • return: Object Super prototype of this Creation.

Returns the super prototype of this Creation.

Author:

Michaelangelo Jong

License:

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.