Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

extendable-base

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

extendable-base

Simple library for setting up Javascript classes based on Backbone.extend

  • 0.3.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

extendable-base: Simple library for setting up Javascript classes Build Status: Linux

Leverages Backbone.extend semantics for defining subclasses in a generic way, making it straightforward to set up javascript class hierarchies.

As a convenience, adds logic so that the default constructor for the Base class calls initialize function on all classes in the inheritance chain passing the same arguments to each class.

Basic Usage

var Base = require('base-extend');

var MyClass = Base.extend({
    initialize: function(name) {
        this.name = name;
    },

    whoami: function() {
        console.log("I am", this.name);
    }
});

console.log(new MyClass('joe').whoami());
// 'I am joe'

Inheritance

Simple inheritance chains work as well, calling initialize for all classes in the chain:

var Animal = Base.extend({
    initialize: function(type) {
        this.classname = "Animal";
        this.type = type;
    },

    ident : function() {
        return 'I am an ' + this.classname + ': ' + this.type;
    },

    legs : function() {
        return 'I have ' + this._legs + ' legs';
    }
});

var FourLegged = Animal.extend({
    // Initialize is called for all classes
    initialize : function(type) {
        this._legs = 4;
    }
});

var dog = new FourLegged('dog');

console.log(dog.ident());
// 'I am an Animal: dog'

console.log(dog.legs());
// 'I have 4 legs'

Extending existing types

You can use Base.extends to subclass an existing Javascript type, for example to define custom Error subclasses:

var BaseError = Base.inherits(Error, {
    default_status: 500,

    initialize: function(message, status) {
        Error.call(this, message);

        this.message = message || this.default_message || "";
        this.status = status || this.default_status;
    },

    toJSON: function() {
        return {status: this.status, message: this.message};
    }
});

var NotFoundError = BaseError.extend({
    default_status: 404,
    default_message: "Bad Request"
});

var BadRequestError = BaseError.extend({
    default_status: 400,
    default_message: "Not Found"
});

console.log(new BadRequestError().toJSON());
// { status: 400, message: 'Not Found' }

console.log(new BadRequestError("Invalid request").toJSON());
// { status: 400, message: 'Invalid request' }

console.log(new BadRequestError("Unauthorized", 401).toJSON());
// { status: 401, message: 'Unauthorized' }

console.log(new NotFoundError().toJSON());
// { status: 404, message: 'Bad Request' }

Keywords

FAQs

Package last updated on 20 Apr 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