Coder Social home page Coder Social logo

rmoop's Introduction

#Javascript Inheritance and Interfaces using the Revealing Module Pattern

The revealing module pattern (link) is a technique used in Javascript to create closures or constructors allowing a simple and clean use of encapsulation inside each module.

Always has been a discussion between the use of modules and prototypes, and one of the cons that have modules is that developers argue that you can apply inheritance using the prototype chain instead of modules that don't support this functionality.

For that reason, I introduce RMOOP, a js library to integrate easily this functionality to the revealing module pattern.

##RMOOP

In RMOOP (Revealing Module OOP) you have available 3 methods to apply inheritance and interfaces:

  • extend: Where you can extends a single or multiple modules (YES, it supports multiple inheritance).
  • export: Expose private methods as public.
  • implement: Attach an interface to the module.

Below you can see an example of the library in action:

var IShape = {
	draw: function(){},
	erase: function(){}
};

var Shape = function () {
	var color = 'red',
		stroke = 1;

	//	implements an interface
	this.implement(IShape);


	function hide() {
		return 'Hiding Shape';
	}

	/**
	 * Draws a shape
	 * implements an interface method - case 1
	 */
	function draw() {
		return "Drawing a Shape";
	}

	/**
	 * Erases the shape
	 * implements an interface method - case 2
	 */
	this.erase = function () {
		return false;
	}

	/**
	 * Some private method
	 * @private
 	*/
	function privateMethod() {

	}

	return this.exports(this,{
		color: color,
		hide: hide,
		draw: draw
	});
};

/**
 * Represents a rectangle
 * extends a Shape
 */
var Rectangle = function () {
	var width = 10,
		height = 5,
		_super = this.extend(Shape);

	/**
	 * Draws the rectangle
	 */
	this.draw = function() {
		return "Drawing Rectangle -> " + _super.draw();
	};

	return this.exports(this, {});
};

If you found interesting this approach, or you want to contribute, there is a repository to download the code:

https://github.com/jandrade

See you soon.

rmoop's People

Contributors

jandrade avatar

Stargazers

 avatar

Watchers

 avatar  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.