Socket
Socket
Sign inDemoInstall

generate-js

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

generate-js

An easy to use prototypal inheritance model and generator.


Version published
Weekly downloads
4.2K
decreased by-24.26%
Maintainers
1
Weekly downloads
 
Created
Source

Table of Contents

Generator

An easy to use prototypal inheritance model and generator. All generations inherit from Generator.

Install:

$ npm install generate-js

Generator.generate(create)

  • create Function Create method that gets called when creating a new object that inherits from this generator.
  • return: Generator A new generator that inherits from this generator.

Returns a new generator that inherits from Generator.

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 or its generator was generated by Generator, false otherwise.

Generator.toGenerator(constructor)

  • constructor Function An constructor to be generatorized.
  • return: Generator A new generator that who's create method is equivalent to calling new constructor();.

Returns a new generator that is equivalent to constructor.

NOTE: Most native constructors can NOT be generatorized.

Example:

// generatorize NodeJS' EventEmitter
var Generator = require('generate-js'),
	events    = require('events');

var EventEmitter = Generator.toGenerator(events.EventEmitter);

// EventEmitter.create() same as new events.EventEmitter();

// new generators can inherit all the abilities of EventEmitter like so.
var MyNewGenerator = EventEmitter.generate(
	/* create method */
	function MyNewGenerator() {}
);

Class: Generation

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

Generation.name

  • name String The name of the create method.

Name property for this generator.

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 generator's prototype.
  • return: Generator This generator.

Defines shared properties for all objects created by this generator.

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.create()

  • return: Object A new object that inherits from this generator.

Creates a new object that inherits from this generator.

Example:

var jim = Person.create('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.generate(create)

  • create Function Create method that gets called when creating a new object that inherits from this generator.
  • return: Generator A new generator that inherits from this generator.

Returns a new generator that inherits from this generator.

Example:

var Student = Person.generate(
	/* create method */
	function Student(name, age, sex, studentId) {
		// '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.
		this.supercreate(name, age, sex);
		this.studentId = studentId || 'A0000000000';
	}
);

Student.definePrototype(
	{
		writable: true
	},
	{
		sayHello: function () {
			console.log('Sup? My student ID is: ' + this.studentId + '.');
		}
	}
);

var sarah = Student.create('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 object or its generator was generated by this generator, false otherwise.

Generation.isCreation(test)

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

Returns true if test was created by this generator or its generations, false otherwise.

Class: Creation

A new object that inherits from the generator that created it using the Generation.create() method.

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 object.
  • return: Object This object.

Defines properties on this object.

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'
	}
);

Author:

Michaelangelo Jong

License:

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.

Keywords

FAQs

Package last updated on 06 Mar 2015

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc