Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

classyobjects.js and readme example

  • Loading branch information...
commit b6bf1f7ed0ccea76c448b37585b15ed7b34338a3 1 parent 18161c2
@derickbailey authored
Showing with 123 additions and 0 deletions.
  1. +86 −0 classyobjects.js
  2. +37 −0 readme.md
View
86 classyobjects.js
@@ -0,0 +1,86 @@
+// ------------------------------
+// ClassyObjects.js
+// Copright (C) 2012 Derick Bailey, Muted Solutions, LLC
+//
+// A class-y framework for prototypal inheritance in JavaScript
+//
+// For more information, see
+// WatchMeCode Episode 5: JavaScript Objects & Prototypes
+// http://watchmecode.net/javascript-objects
+// -------------------------------
+
+
+// This function is directly borrowed from
+// [Douglas Crockford](http://javascript.crockford.com/prototypal.html)
+if (!Object.create || !typeof Object.create === "function"){
+ Object.create = function(obj){
+ var F = function(){};
+ F.prototype = obj;
+ return new F();
+ }
+}
+
+// The ClassyObjects Namespace
+// ---------------------------
+//
+var ClassyObjects = {};
+
+// CopyTo
+// ------
+
+// Copies the key/value pairs (methods and attributes)
+// from a source object to a target object. Loosely based
+// on the implementation of Underscorejs' `extend`
+// functionality.
+ClassyObjects.copyTo = function(target, source){
+ for(var attr in source){
+ target[attr] = source[attr];
+ }
+}
+
+// Inherits
+// --------
+
+// Create an "class" that inherits from the object you
+// specify, and is augmented with the object definition
+// provided.
+ClassyObjects.inherits = function(inherited, definition){
+ var inheritedInstance = Object.create(inherited);
+
+ ClassyObjects.copyTo(inheritedInstance, definition);
+
+ var ClassConstructor = function(){
+ this.super = inherited;
+ };
+ ClassConstructor.prototype = inheritedInstance;
+
+ return ClassConstructor;
+}
+
+// Extend
+// ------
+
+// Add this method to an existing object, and you can
+// then extend from that object to create a new "class"
+// that inherits from it.
+ClassyObjects.extend = function(definition){
+ var prototype = this;
+ if (typeof this === "function")
+ prototype = this.prototype;
+ var Constructor = ClassyObjects.inherits(prototype, definition);
+ Constructor.extend = this.extend;
+ return Constructor;
+}
+
+// Define
+// ------
+
+// Define a new "class" with the object definition that
+// you provide. It will have the `extend` function attached
+// to it, so that you can inherit / extend from the
+// resulting "class".
+ClassyObjects.define = function(definition){
+ var classyObj = {};
+ classyObj.extend = ClassyObjects.extend;
+ return classyObj.extend(definition);
+}
View
37 readme.md
@@ -23,6 +23,43 @@ with objects and prototypes in JavaScript.
For more information on how I built it, and why it looks
the way it does, check out the screencast (paid).
+## Example Usage
+
+```js
+
+// Define a new "class" (constructor function)
+// -----------------------------------
+
+var Foo = ClassyObjects.define({
+ widget: function(){
+ console.log("widget");
+ }
+});
+
+// Extend this "class" to create another
+// ------------------------------------
+var Bar = Foo.extend({
+ bar: "baz",
+
+ quux: function(){
+ console.log("whatever");
+ },
+
+ widget: function(){
+ this.super.widget();
+ console.log("widget modified");
+ }
+});
+
+// Use the objects that we defined
+// ------------------------------------
+
+var b = new Bar();
+console.log(b.bar);
+b.quux();
+b.widget();
+```
+
## Unsupported, Bug-Filled, Example Code
This project is not suitable for use in any production
Please sign in to comment.
Something went wrong with that request. Please try again.