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

Class

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

Class

Port of Prototype.js inheritance implementation for Node.js.

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
156
decreased by-7.69%
Maintainers
1
Weekly downloads
 
Created
Source

Class

Class.create returns a function that, when called, will fire its own initialize method.

Class.create accepts two kinds of arguments. If the first argument is a Class, it's treated as the new class's superclass, and all its methods are inherited. Otherwise, any arguments passed are treated as objects, and their methods are copied over as instance methods of the new class.

If a subclass overrides an instance method declared in a superclass, the subclass's method can still access the original method. To do so, declare the subclass's method as normal, but insert $super as the first argument. This makes $super available as a method for use within the function.

Special properties

Classes themselves contain several special properties:

  • The superclass property refers to an class's superclass (or null if there is no superclass).
  • The subclasses property stores an array of all a class's subclasses (or an empty array if it has none).

In addition, an instance of a class contains the native JavaScript constructor property, which refers to the class that created it.

Installing

GIT

$ git clone https://github.com/firejune/class.git

NPM

$ npm install Class

Code

var Animal = Class.create({
  initialize: function(name, sound) {
    this.name  = name;
    this.sound = sound;
  },

  speak: function() {
    alert(this.name + " says: " + this.sound + "!");
  }
});

// subclassing Animal
var Snake = Class.create(Animal, {
  initialize: function($super, name) {
    $super(name, 'hissssssssss');
  }
});

var ringneck = new Snake("Ringneck");
ringneck.speak();
//-> alerts "Ringneck says: hissssssssss!"

var rattlesnake = new Snake("Rattler");
rattlesnake.speak();
//-> alerts "Rattler says: hissssssssss!"

Have fun!

License

MIT <3

Keywords

FAQs

Package last updated on 10 Sep 2011

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