New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

sclass.js

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sclass.js

Small JS utility for defining classes, performing inheritance and implementing interfaces.

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-70%
Maintainers
1
Weekly downloads
 
Created
Source

sClass.js - Simply to create Class in JS

Stable version - v1.0.1

Build Status

Small JS utility for defining classes, performing inheritance and implementing interfaces. It is easy to use and supported IE8 and all modern browsers.

Installation

Node.js

npm install sclass.js --save

Bower

bower install sclass.js --save

Using by browser


<script type="text/javascript" src="path_to_sclass/index.min.js"></script>

Using by CommonJS (Node.js)


var $Class = require("sclass.js").$Class;

Creating class


var MyClass = $Class(); // creating class

MyClass.prototype.$constructor = function(id) {
  // this function is applied in its context during performing statement: new MyClass()
  this.id = id;
  this.text = "Good evening ... infidels!";
};

MyClass.prototype.getText = function() {
  return this.text;
};

MyClass.prototype.getId = function() {
  return this.id;
};

var instance = new MyClass(1); // creating instance of class
console.log(instance.getText()); // "Good evening ... infidels!"
console.log(instance.getId()); // 1

Creating singleton


var DeathStar = $Class({ 
  singleton : true 
});

DeathStar.prototype.$constructor = function(id) { 
  this.weapons = [];
};

DeathStar.prototype.getWeapons = function() {
  return this.weapons;
};

console.log(DeathStar.getInstance().getWeapons()); // []

// you can not call singleton with new, but always with getInstance method
// this throws an error
var dStar = new DeathStar(); 

Performing inheritance


var Knight = $Class();

Knight.prototype.$constructor = function() {
  this.weapon = "saber";
};

Knight.prototype.getWeapon = function() {
  return this.weapon;
};



var JediKnight = $Class({
  extending: Knight // you can see we give reference to Knight
});

JediKnight.prototype.$constructor = function() {
  this.weapon = "lightsaber";
};

var jKnight = new JediKnight();
console.log(jKnight instanceof JediKnight); // true
console.log(jKnight instanceof Knight); // true
console.log(jKnight.getWeapon()); // shows "lightsaber"

Using $super method


var Knight = $Class();

Knight.prototype.$constructor = function(name) {
  this.name = name || "noname";
  this.weapon = "saber";
};

Knight.prototype.getWeapon = function() {
  return this.weapon;
};

Knight.prototype.setWeapon = function(name) {
  this.weapon = name;
};



var JediKnight = $Class({
  extending: Knight // you can see we give reference to Knight
});

JediKnight.prototype.$constructor = function(name, skill) {
  this.$super(name); // just simply call $super to apply parent $constructor
  this.skill = skill || 0;
  this.weapon = "";
  this.weaponSkill = 0;
};

JediKnight.prototype.setWeapon = function(name, skill) {
  this.$super(name); // just simply call $super to apply parent setWeapon
  this.weaponSkill = skill;
};

JediKnight.prototype.getSkill = function() {
  return this.skill;
};

JediKnight.prototype.getWeaponSkill = function() {
  return this.weaponSkill;
};

var jKnight = new JediKnight("Obi-Wan Kenobi", 5);
jKnight.setWeapon("lightsaber", 10);
console.log(jKnight.getSkill()); // 5
console.log(jKnight.getWeapon()); // "lightsaber"
console.log(jKnight.getWeaponSkill()); // 10

Implementing interfaces


var IFaceWeapons = {
  setWeapon: function(name) {
    this.name = name;
  },
  getWeapon: function() {
    return this.name;
  }
};

var IFaceSkills = {
  setSkill: function(skill) {
    this.skill = skill;
  },
  getSkill: function() {
    return this.skill;
  }
};

var Knight = $Class({
  implementing: IFaceWeapons
});

Knight.prototype.$constructor = function(name) {
  this.name = name || "noname";
};



var JediKnight = $Class({
  extending: Knight,
  implementing: IFaceSkills
}); // we use parent $constructor, we can not define it again

var jKnight = new JediKnight("Obi-Wan Kenobi");

jKnight.setWeapon("lightsaber");
jKnight.setSkill(10);

console.log(jKnight.getWeapon()); // "lightsaber"
console.log(jKnight.getSkill()); // 10

Implementing multiple interfaces


var Sith = $Class({
  extending: Knight,
  implementing: [IFaceWeapons, IFaceSkills] // first IFaceWeapons is implemented and then IFaceSkills
});

Keywords

FAQs

Package last updated on 18 Sep 2014

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