Skip to content

Instantly share code, notes, and snippets.

@harmesy
Created April 15, 2013 17:38
Show Gist options
  • Save harmesy/5389847 to your computer and use it in GitHub Desktop.
Save harmesy/5389847 to your computer and use it in GitHub Desktop.
Just playing around with creating quasi-classical objects in JS. The larger and smaller objects aren't the most readable in the world, just some messing around.
#!/usr/bin/env node
Object.prototype.createClass = function(obj) {
var self = {};
var publicInterface = (obj['inherits'] && obj['inherits']()) || {};
return (function() { return obj.func(self, publicInterface) });
}
var larger = Object.createClass({ func: function(self, publicInterface) {
self.largerVar = "Larger";
publicInterface.getLargerVar = function() {
return self.largerVar;
};
return publicInterface;
}});
var smaller = Object.createClass({ inherits: larger, func: function(self, publicInterface) {
self.smallerVar = "Smaller";
publicInterface.getSmallerVar = function() {
return self.smallerVar;
}
return publicInterface;
}});
var bigg = function() {
var self = {};
var publicInterface = {};
self.status = "Good";
publicInterface.getStatus = function() {
return self.status;
};
publicInterface.setStatus = function(status) {
self.status = status;
};
return publicInterface;
};
var little = function() {
var self = {};
var publicInterface = bigg();
self.name = "Matt";
publicInterface.getName = function() {
return self.name;
};
publicInterface.setName = function(name) {
self.name = name;
};
return publicInterface;
};
var myBigg = bigg();
var myLittle = little();
var myLarger = larger();
var mySmaller = smaller()
console.log(myBigg.getStatus());
console.log(myLittle.getStatus());
console.log(myLittle.getName());
console.log(myLarger.getLargerVar());
console.log(mySmaller.getLargerVar());
console.log(mySmaller.getSmallerVar());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment