/*
First we create our "Class", and name it thoroughly to enable .constructor calls.
This function is, besides being the Class itself, also the function that is run whenever an instance of the Class is instanciated. To keep our code in one spot, we simple make this call a construct function on itself.
*/
var Klass = function Klass () {
return this.construct.apply(this, arguments);
};
/*
Now we define our prototype. This is basically the collection of private and public methods and variables that every instance of our class will have available
*/
Klass.prototype = (function () {
var construct, privath, // public methods
instances, // private vars
publich, privathWrap, privathWrapWithScope; // public methods
instances = 0;
/*
Constructor
*/
construct = function () {
var scope = this; // This is our instance
scope.value = 0;
instances += 1;
console.log('Klass initiated, instance number' + instance);
};
/*
Public
*/
publich = function () {
console.log('publich', this, arguments);
};
/*
Private
*/
privath = function () {
console.log('privath', this, arguments);
};
/*
privathWrap
*/
privathWrap = function () {
privath(arguments);
};
/*
privathWrapWithScope
*/
privathWrapWithScope = function () {
privath.apply(this, arguments);
};
/*
Now we reveal all our public methods to the prototype, to enable these to be available on every instance
*/
return $.extend(Klass.prototype, { // We use jQuery.extend to retain the original prototype object members
construct: construct, // We most reveal this to allow the trick we made with the constructor when creating the Class to work
publich: publich,
privathWrap: privathWrap,
privathWrapWithScope: privathWrapWithScope
});
}());
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.