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

basejs

v1.1.1

Published

Simple class-based inheritance for JavaScript (based on Dean Edwards' Base.js).

Downloads

90

Readme

Base.js Module

This is a modular version of Dean Edward's Base.js. The script first checks to see if it is in an AMD environment and if it is defines an AMD module. If the current environment is not an AMD environment then the script checks to see if it is in a web browser and attaches the Base constructor to the window object. If those two tests fail then it is assumed that the script is running in a CommonJS environment and it assigns module.exports to be a reference to the Base constructor.

In addition to modularizing the code I have also updated the entire implementation so that it passes JSHint (with default settings in the installed version -- the web version turns on strict by default).

Getting Started

The first way to get started using the script is to simply include it wherever else you include scripts (be it at the top, bottom, through a script loader, or what have you):

<script src="/path/to/Base.js"></script>

The second way to get started is to include the script as a dependency for an AMD application or module (using something such as RequireJS):

:::javascript
require('/path/to/Base', function (Base) {
	// You may now use Base.js as "Base" here.
	// Remember, AMD modules don't use the .js extension!
});

The third and final way to get started is to require the module in a CommonJS environment (such as node.js):

:::javascript
var Base = require('./path/to/Base');

If you are using node.js with npm then you can also install the module as follows:

:::text
npm install basejs

Then in your script you only need to do the following:

:::javascript
var Base = require('basejs');

Defining Classes

Moving forward, whichever path you chose above you should now have a Base object to work with. Basic usage is as follows:

:::javascript
// Define Animal class
var Animal = Base.extend({
	constructor: function (name) {
		this.name = name;
	},
	name: '',
	eat: function () {
		this.say('Yum!');
	},
	say: function (message) {
		console.log((this.name !== '' ? this.name + ': ' : this.name) + message);
	}
});

// Define the Cat class which extends Animal
var Cat = Animal.extend({
	eat: function (food) {
		if (food instanceof Mouse)
			this.base();
		else
			this.say('Yuk! I only eat mice.');
	}
});

// Define the a few more animals
var Mouse = Animal.extend();
var Dog = Animal.extend();

// Try out the classes
var kitty = new Cat('Kitty');
kitty.eat(new Mouse()); // logs "Yum!"
kitty.eat(new Dog()); // logs "Yuk! I only eat mice."

Notice the use of this.base(). this.base() is how you access the parent's method. This works in all methods (including constructors).

You can also define static (class-level) properties and methods:

:::javascript
var Circle = Shape.extend({ // instance properties / methods
	constructor: function (x, y, radius) {
		this.base(x, y);
		this.radius = radius;
	},
	radius: 0,
	getCircumference: function () {
		return 2 * Circle.PI * this.radius;
	}
}, { // class properties / methods
	PI: 3.14
});

For more details including private data and singleton objects check out Dean Edward's original documentation.

License

The MIT License

Based on Base.js 1.1a (c) 2006 - 2010 Dean Edwards
Updated to pass JSHint and converted into a Module format by Kenneth Powers

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.