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

oloo-factory-creator

v0.2.2

Published

Creates factory functions for objects implementing the OLOO pattern

Downloads

4

Readme

OLOO Factory Creator

This library allows for the creation of factory functions for objects implementing the OLOO pattern (Objects Linking to Other Objects). This is an alternative to classes for object orientation. See You Don't Know JS: this & Object Prototypes Chapter 6: Behavior Delegation for more information about the pattern.

Essentially this library allows a normal object notation syntax to be used for instantiation and inheritance instead of the horrible FunctionName.prototype.method = function ()... syntax or the ES6 class syntax and wraps the object creation up in a factory function. This has convenient functionality such as assigning properties automatically onto the new object and also hides the implementation details of how the object was created.

Installation

Node

Requires Node 4 due to requiring Object.assign().

npm install oloo-factory-creator

Browser

Download dist/create-factory.min.js or you can install with bower:

bower install oloo-factory-creator

Usage

In Node, import the createFactory function:

var createFactory = require('oloo-factory-creator');

In the browser, import via the script tag, which creates a global function called createFactory:

<script src="/path/to/dist/create-factory.min.js" type="text/javascript"></script>

Automatically assign properties into the created objects

The easiest method of creating objects is just to pass an object (or multiple objects) into the factory that will have their properties copied into the created object.

// The prototype for user objects
var User = {
    getName: function () {
        return this.name;
    }
};

var userFactory = createFactory(User);

// Create user objects with User as their prototype
var bob = userFactory({
    name: 'Bob'
});

var jim = userFactory({
    name: 'Jim'
});

// Accessing copied properties
console.log(bob.name); // Outputs "Bob"
console.log(jim.name); // Outputs "Jim"

// Calling prototyped methods
console.log(bob.getName()); // Outputs "Bob"
console.log(jim.getName()); // Outputs "Jim"

If you need to run some code after the object has been initialised with the passed in properties, you can specify a postInit() method in the prototype:

var User = {
    postInit: function() {
        console.log('My name is ' + this.name);
    }
};

var userFactory = createFactory(User);

// This outputs "My name is Bob"
var bob = userFactory({
    name: 'Bob'
});

Initialising created objects with an init() method

As an alternative, if an init() method is specified in the prototype, that will be used and properties will not be automatically assigned.

var User = {
    init: function (name) {
        this.name = name;
    },
    getName: function () {
        return this.name;
    }
};

var bob = userFactory('Bob');

console.log(bob.name); // Outputs "Bob"
console.log(bob.getName()); // Outputs "Bob"

Modules

With either of the above methods, if you're using modules, the recommended usage is to export the created factory method. For example, user.js could look like the following:

var createFactory = require('oloo-factory-creator');

var User = {
    ...methods
};

module.exports = createFactory(User);

And then imported into in another module thus:

var User = require('./user');

var user = User({
    name: 'Bob'
});

console.log(user.getName());

Inheritance

In OLOO there is no difference between inheritance and instantiation.

Testing

Simply clone the repository, run npm install and then run npm test. The tests are in tests/create-factory-spec.js.