npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nature-js

v0.5.5

Published

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

Downloads

37

Readme

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.