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

jay-extend

v1.0.1

Published

A super fast prototypal inheritance microlib

Downloads

40

Readme

Jay Extend

A super fast prototypal inheritance microlib for the modern web.

Rationale

Why another JavaScript inheritance library? In a word: Speed.

The world is littered with JavaScript inheritance libraries. Every framework has its own builtin flavor. This microlib takes a slightly different approach, providing the fastest possible inheritance implementation while being easy to use.

Jay Inheritance is very similar to John Resig's Simple Inheritance pattern (on which it is based), but with improvements in constructor-time, and run-time performance.

Caveat Emptor: Startup-time performance suffers, due to Object.defineProperty calls.

More information

I wrote an in-depth article that is available on my blog.

Basic usage

Setup in nodeland

// Require Jay.extend() method
var Jay = require("jay-extend");

Setup in browserland

<!-- Add Jay.extend() method -->
<script src="jay-extend/lib/jay-extend.js"></script>

Example class definitions

var Person = Jay.extend({
    "init" : function (isDancing) {
        this.dancing = isDancing;
    },
    "dance" : function () {
        return this.dancing;
    }
});

var Ninja = Person.extend({
    "init" : function () {
        // Call the super constructor, passing a single argument
        this._super(Person, "init", [ false ] );
    },
    "dance" : function () {
        // Call the overridden dance() method
        return this._super(Person, "dance");
    },
    "swingSword" : function () {
        return true;
    }
});

var Pirate = Person.extend(Ninja, {
    "init" : function() {
        // Call the super constructor, passing a single argument
        this._super(Person, "init", [ true ]);
    }
});

Example class instantiation

var p = new Person(true);
console.log(p.dance()); // => true

var n = new Ninja();
console.log(n.dance()); // => false
console.log(n.swingSword()); // => true

var r = new Pirate();
console.log(r.dance()); // => true
console.log(r.swingSword()); // => true

console.log(
    p instanceof Person &&
    n instanceof Ninja &&
    n instanceof Person &&
    r instanceof Pirate &&
    r instanceof Person
); // => true

Jay.extend

Returns Class

Argument | Type | Description ---------|------|------------ mixins... | Descriptor[] | Each mixin is a dictionary of functions, or a previously extended class whose methods will be applied to the target class prototype.

When multiple mixins are provided, methods may be overridden by later mixins; The last mixin provided takes precedence. To call overridden mixin methods, access its prototype or use this._super() (See below).

Descriptor is a simple (single-level) Object or Class. Each key in the Descriptor must have a Function value.

this._super

Returns Mixed

Argument | Type | Description ---------|------|------------ superClass | Class | Super class to access methodName | String | Method name to access on superClass args... | Mixed[] | List of arguments to pass to the method

To call methods on another Class within the prototype chain (or indeed not even in the prototype chain!) you should use the standard prototypal call pattern:

SuperClass.prototype.methodName.call(this, arg1, arg2, ...);

However, Jay Inheritance includes a syntactic sugar convenience method on the prototype called _super. The above can be rewritten as:

this._super(SuperClass, "methodName", [ arg1, arg2, ... ]);

Which is a little easier to swallow. Note that superClass does not need to be in the prototype chain, which allows mixins to function properly.

Better than this._super

It is slower to call this._super by at least a factor of 3: http://jsperf.com/inheritance-showdown/5

To keep the convenience of syntactic sugar and retain all of the raw performance provided by the JavaScript VM, it's possible to use compile-time replacements with grunt-replace.

Here's a sample grunt-replace configuration that takes care of this for you:

"options" : {
  "patterns" : [
    {
      "match" : /this\._super\(\s*([\w\.]+)\s*,\s*"(\w+)"\s*(,\s*)?/g,
      "replacement" : "$1.prototype.$2.apply(this$3"
    }
  ]
}

License

The source code is hereby released under the MIT License. The full text of the license appears below.

Copyright (c) 2014-2015 Jay Oster

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.