Giter VIP home page Giter VIP logo

class-256.js's Introduction

class-256.js

npm version Bower version devDependency Status Travis

class-256.js is a less than 256 byte JavaScript classical inheritance pattern library (249 bytes minified or 391 bytes minified with UMD pattern).
Originally based on augment and extend.

Features

  • OOP style declaration
  • Constructor methods (optional - if you don't provide one, the parent's constructor will be called upon instantiation)
  • Working instanceof
  • Public and private/privileged properties/methods
  • Non-overridden public properties/methods defined in parent naturally accessible
  • Overridden public methods defined in parent accessible through parent parameter

Basic usage

var HelloWorld = Class.extend(function() { // default name of .extend() can be changed via constant
	this.greeting = 'Hello '; // public property

	var world = 'World!'; // private property

	this.constructor = function(greeting) { // default name of .constructor() can be changed via constant
		if (typeof greeting != 'undefined') {
			this.greeting = greeting;
		}
	};

	this.say = function() { // public method
		return privileged.call(this);
	};

	function privileged() { // private/privileged method
		return this.greeting + world;
	}
});

var helloWorld = new HelloWorld()
helloWorld.say() // 'Hello World!'
helloWorld instanceof HelloWorld // true

var hiWorld = new HelloWorld('Hi ')
hiWorld.say() // 'Hi World!'
hiWorld instanceof HelloWorld // true

Extending

function isNonEmptyString(value) {
	return typeof value == 'string' && value.length > 0;
}

var Base = Class.extend(function() {
	this.name = '';

	this.constructor = function(name) {
		this.name = name;
	};

	// check name
	this.isValid = function() {
		return isNonEmptyString(this.name);
	};
});


var Extended = Base.extend(function(parent) {
	this.address = '';

	this.constructor = function(name, address) {
		parent.constructor.call(this, name); // call parent constructor
		// could be also written as: this.name = name;

		this.address = address;
	};

	// check name and address
	this.isValid = function() {
		return parent.isValid.call(this) && isNonEmptyString(this.address);
		// could be also written as: return isNonEmptyString(this.name) && isNonEmptyString(this.address);
	};
});

var emptyExample = new Extended(null, null);
emptyExample.isValid() // false

var nameExample = new Extended('John', null);
nameExample.isValid() // false

var addressExample = new Extended(null, 'London');
addressExample.isValid() // false

var validExample = new Extended('John', 'London');
validExample.isValid() // true

For more examples see test/test.js.

Installation

NPM

npm install class-256.js

Bower

bower install class-256.js

Browser

<script src="https://github.com/koffeine/class-256.js/blob/master/dist/class.umd.min.js" charset="utf-8"></script>

Files

UMD Minified File
dist/class.umd.min.js
dist/class.min.js
dist/class.umd.js
dist/class.js

Develop

Set up

npm install

Build

Running the following command will start Gulp, which will run ESLint, create the UMD version, run Mocha/Chai tests and run UglifyJS:

gulp

License

Copyright © 2015-2016 Horváth Kornél

Licensed under the MIT License.

class-256.js's People

Contributors

koffeine avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.