Skip to content

Commit

Permalink
classyobjects.js and readme example
Browse files Browse the repository at this point in the history
  • Loading branch information
Derick Bailey committed Apr 10, 2012
1 parent 18161c2 commit b6bf1f7
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
86 changes: 86 additions & 0 deletions classyobjects.js
Original file line number Diff line number Diff line change
@@ -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);
}
37 changes: 37 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b6bf1f7

Please sign in to comment.