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

proto-create

v1.0.1

Published

Provides a syntax for prototypal inheritance that also allows for arrays that inherit from a custom prototype

Downloads

4

Readme

Proto Create

This module produces a function that creates and returns a target object which inherits from another object. The syntax used internally is "Object.create()" and this module is meant to provide a cleaner and better looking solution with the added benefit of "sub-classing" arrays, or effectively having an array inherit from any object (including null). The benefit of this is that you do not have to pollute Array.prototype when a custom array method is desired.

Thank you to kangax for the article on subclassing arrays. I used his method of creating an object and giving it the special characteristics of an array manually. The article can be seen here: http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/

The argument of the function must be an object. The accepted optional properties are:

[ proto ] : the desired prototype of the newly created target object. if [ target ] is an array, [ proto ] defaults to Array.prototype, otherwise it defaults to Object.prototype

[ target ] : The new object to be created. This may be an array. Literal syntax is recommended since a shallow copy of the properties will take place.

[ descriptors ] : The descriptors of the object may be defined here.

If both [ target ] and [ descriptors ] are not defined, the function will return an empty object.

Install

npm install proto-create

Usage

const create = require("proto-create");

// creating an object that inherits from Object.prototype to be used as a new prototype
let myObjectPrototype = create({
    proto: Object.prototype,
    target: { a: 1 }
});
// creating an object that inherits from "myObjectPrototype"
let x = create({
    proto: myObjectPrototype,
    target: { b: 2 }
});
console.log(x.b, x.a, x.toString); // 2, 1, [Function: toString]

// creating an object to be used as a prototype for an array, which inherits from Array.prototype
let myArrayPrototype = create({
    proto: Array.prototype,
    descriptors: {
        last: {
            value: function () {
                return this[this.length - 1];
            }
        }
    }
});
// creating an array which inherits from "myArrayPrototype", which in turn inherits from Array.prototype
let y = create({
    proto: myArrayPrototype,
    target: [32, 43, 54, 65]
});
console.log(y.last(), y.map(x => 2 * x)); // 65, [ 64, 86, 108, 130 ]