Coder Social home page Coder Social logo

chris-t-reilly / naturejs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from carlosouro/naturejs

0.0 2.0 0.0 37 KB

Class system for Javascript supporting private and public scopes, multiple inheritance and class packages

License: MIT License

JavaScript 100.00%

naturejs's Introduction

NatureJS

nature-js NPM package information

nature-js travis-CI build nature-js test coverage nature-js bower component

NatureJS is an easy to use Class system for Javascript supporting private, protected and package scopes, and inheritance. Supported in any Ecma5 compliant javascript environment (node.js or browser).


Quick examples:


Create a Class:

var MyCLass = nature.create(definition);

var Foo = nature.create(function(pub, prot){

	//constructor
	//prot scope is protected
	prot.construct = function(name){
		prot.name = name;
	}

	pub.present = function(){
		console.log("Hello. My name is "+prot.name+".")
	}

});

var bar = new Foo("John");
bar.present(); //logs "Hello. My name is John."

Create an instance Factory:

var myFactory = nature.factory(definition);

factory() works the same logic create(), but generates a factory function that returns "function" instances (executing prot.scope()), whereas .create() returns regular classes. Factories can be derived and chained in inheretance or packages just like any Class.

var testFactory = nature.factory(function(pub, prot){

	//constructor
	prot.construct = function(name){
		prot.name = name;
	}

	pub.present = function(){
		console.log("Hello. My name is "+prot.name+".")
	}

	//prot.scope() - method to be run every time the instance function is called
	prot.scope = function(){
		console.log("JS, the place where functions can be instances")
	}

});

var bar = testFactory("John"); //no "new" constructor needed
bar.present(); //logs "Hello. My name is John."
bar(); //logs "JS, the place where functions can be instances"

Multiple inheritance:

var MyCLass = nature.from([ParentN[, ... Parent2], ] Parent1).create(definition);

var Bar = nature.from(Foo).create(function(pub, prot){

	pub.greet = function(whom){
		console.log("Hi "+whom+"!. I'm "+prot.name+".");
	}

});

var baz = new Bar("Carlos");
baz.present(); //logs "Hello. My name is Carlos."
baz.greet("Chris"); //logs "Hi Chris! I'm Carlos."

Note: Multiple inheritance is mainly suited for small or, at most, average dependency graphs - if your application has a very complex class structure consider keeping to single inheritance, otherwise you risk running into multiple inheritance issues. NatureJs, when given the same property declared in multiple inerited parents, will always allow the right-most (ParentN argument) to override. You've been warned.


Non-inheritable private instance scopes:

If you want absolutely private scopes (inheriting classes cannot change), you can use the variety function scope for private static, and the spawn function scope for instance private.

var Baz = nature.create(function(pub, prot){

	var privateText = "happy!";

	pub.sayHappy = function(){
		console.log("I'm "+privateText+"!");
	}

});
var Qux = nature.from(Baz).create(function(pub, prot){

	var privateText = "angry!";

	pub.sayAngry = function(){
		console.log("I'm "+privateText+"!");
	}

});


var qux = new Qux();
qux.sayAngry(); //logs "I'm angry!"
qux.sayHappy(); //logs "I'm happy!"

Packages

var myPackage = nature.createPackage();

var pack = nature.createPackage();

var Foo = pack.create(function(pub, prot, unfold){

	prot.bar = "hi!";

});
var Baz = pack.create(function(pub, prot, unfold){

	pub.logProtectedBar = function(obj){

		var instanceProt = unfold(obj); //Note: throws error in case of out of package obj

		console.log( instanceProt.bar );
	}

});


var foo = new Foo();
var baz = new Baz();

baz.logProtectedBar(foo); //logs "hi!"

SubPackages

var mySubPackage = pack.createPackage();

Subpackages can access all the parent packages instances protected scopes.


Legal Stuff

MIT license. See the included LICENSE file for more details.

naturejs's People

Contributors

carlosouro avatar

Watchers

James Cloos 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.