Permalink
Browse files

Add readme with examples and new syntax

  • Loading branch information...
1 parent 8d80256 commit 306b7e341b7302ebe5a0a543364d9f02bac2c0fb @pazguille committed Apr 6, 2012
Showing with 216 additions and 123 deletions.
  1. +79 −0 README.md
  2. +26 −50 index.html
  3. +0 −73 oop.js
  4. +111 −0 oops.js
View
79 README.md
@@ -0,0 +1,79 @@
+Opps! JS
+==========
+Oops! JS is an Object Oriented JavaScript Library.
+
+# Syntax
+--------
+
+``` js
+var Animal = oops.Class("Animal")
+ .init(function (conf) {
+ this.species = "animal";
+ this.name = conf.name;
+ })
+ .add({
+ "getSpecies": function () {
+ console.log(this.species);
+ },
+ "getName": function () {
+ console.log("My name is " + this.name);
+ }
+ })
+ .build();
+
+var Dog = oops.Class("Dog")
+ .inherit(Animal)
+ .init(function (conf) {
+ this.species = "canine";
+ })
+ .add({
+ "bark": function () {
+ console.log("Wua bark woop arf");
+ }
+ })
+ .build();
+
+var Boxer = oops.Class("Boxer")
+ .inherit(Dog)
+ .add({
+ "bark": function () {
+ console.log("Wuuuuuuuuuuuuua");
+ Boxer.parent.bark.call(this);
+ }
+ })
+ .build();
+
+var foo = new Boxer({name: "foo"});
+
+foo.getName(); // "My name is foo"
+foo.getSpecies(); // "canine"
+```
+
+# Contact
+---------
+- Guillermo Paz (Frontender)
+- e-mail: guille87paz (at) gmail.com
+- Twitter: @pazguille
+
+
+# License
+---------
+Copyright (c) 2012
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
View
76 index.html
@@ -1,74 +1,50 @@
<!DOCTYPE html>
<html>
<head>
- <title>Oop.JS: Object Oriented JavaScript Class Library</title>
+ <title>Oops! JS: Object Oriented JavaScript Library</title>
</head>
<body>
- <script src="http://code.jquery.com/jquery.min.js"></script>
- <script src="oop.js"></script>
+ <script src="oops.js"></script>
<script>
- var Animal = oop.Class("Animal")
+ var Animal = oops.Class("Animal")
.init(function (conf) {
+ this.species = "animal";
this.name = conf.name;
})
- .add("sleep", function () {
- console.log("ZzZzZzZzZzZzZzZzZz");
- console.log(this.name);
- })
- .add("getName", function () {
- console.log("Mi nombre es " + this.name);
+ .add({
+ "getSpecies": function () {
+ console.log(this.species);
+ },
+ "getName": function () {
+ console.log("My name is " + this.name);
+ }
})
.build();
- var Cat = oop.Class("Cat")
- .extends(Animal)
- .init(function () {
- this.test = "some cat";
- console.log("Create " + this.test);
- })
- .add("purr", function () {
- console.log("purrrrrrrrrrrrr");
- })
- .add("sleep", function () {
- console.log("MiaaaaaaaaaaaauZzZzZzZzZzZzZzZzZz");
- })
- .build();
-
- var Dog = oop.Class("Dog")
- .extends(Animal)
- .add("bark", function () {
- console.log("Wua bark woop arf");
+ var Dog = oops.Class("Dog")
+ .inherit(Animal)
+ .init(function (conf) {
+ this.species = "canine";
})
- .add("sleep", function () {
- console.log("Wua bark woop arfzzzzzzzzzzzzzzzzzzzzz");
- Dog.parent.sleep.call(this);
+ .add({
+ "bark": function () {
+ console.log("Wua bark woop arf");
+ }
})
.build();
- var Boxer = oop.Class("Boxer")
- .extends(Dog)
- .add("sleep", function () {
- console.log("AUUUUUUUUUUHHHHHHHH");
- Boxer.parent.sleep.call(this);
+ var Boxer = oops.Class("Boxer")
+ .inherit(Dog)
+ .add({
+ "bark": function () {
+ console.log("Wuuuuuuuuuuuuua");
+ Boxer.parent.bark.call(this);
+ }
})
.build();
-
-
-
var foo = new Boxer({name: "foo"});
- foo.sleep();
- var luz = new Dog({name: "Luz"});
- var lilo = new Cat({name: "lilo"});
- lilo.getName(); // Lilo
- lilo.sleep(); // ZzZzZzZzZzZzZzZzZz
- lilo.purr(); // purrrrrrrrrrrrr
-
- var luz = new Dog({name: "Luz"});
- luz.getName()
- luz.sleep(); // ZzZzZzZzZzZzZzZzZz
- luz.bark(); // Wua bark woop arf
</script>
</body>
</html>
View
73 oop.js
@@ -1,73 +0,0 @@
-(function(window, undefined) {
-
- Object.create = Object.create || (function(){
- var F = function(){};
- return function (obj) {
- F.prototype = obj;
- return new F();
- };
- }());
-
- var oop = (function(){
-
- var Class = function (klass) {
- var Klass, definitor;
-
- Klass = function (conf) {
- // Forgot "new" prefix?
- var that = (this instanceof Klass) ? this : Object.create(Klass.prototype);
- var conf = conf || {};
- that.type = klass;
-
- if (Klass.parent && Klass.parent["init"]) {
- Klass.parent.init.call(that, conf);
- }
-
- if (this["init"]) {
- this.init.call(that, conf);
- }
-
- return that;
- };
-
- definitor = {
-
- "extends": function (Parent) {
- Klass.prototype = Object.create(Parent.prototype);
- Klass.prototype.constructor = Klass;
- Klass.parent = Parent.prototype;
-
- return definitor;
- },
-
- "init": function (func) {
- Klass.prototype.init = func;
-
- return definitor;
- },
-
- "add": function (name, fn) {
- Klass.prototype[name] = fn;
-
- return definitor;
- },
-
- "build": function () {
- return Klass;
- }
-
- };
-
- return definitor;
-
- };
-
- var core = {
- "Class": Class
- };
-
- return core;
-
- })();
-
- window.oop = oop;
View
111 oops.js
@@ -0,0 +1,111 @@
+;(function (exports) {
+ "use strict";
+
+ Object.create = Object.create || (function(){
+ var F = function () {};
+ return function (obj) {
+ F.prototype = obj;
+ return new F();
+ };
+ }());
+
+ var oops = (function () {
+
+ var clone = function (o) {
+ var obj = {}, x;
+ for (x in o) {
+ obj[x] = o[x];
+ };
+ return obj;
+ };
+ var Class = function (klass) {
+
+ var Klass = function (conf) {
+
+ var that = this,
+ conf = conf || {},
+ prop,
+ arg = 0;
+
+ if (Klass.parent) {
+ Klass.parent.call(that, conf);
+ Klass.parent = clone(that);
+ }
+
+ that.type = klass;
+
+ if (Klass.mixin) {
+ for (arg; arg < Klass.mixin.length; arg += 1) {
+ for (prop in Klass.mixin[arg]) {
+ if (Klass.mixin[arg].hasOwnProperty(prop)) {
+ that[prop] = Klass.mixin[arg][prop];
+ }
+ }
+ }
+ }
+
+ if (Klass["init"]) {
+ Klass["init"].call(that, conf);
+ }
+
+ if (Klass.members) {
+ for (prop in Klass.members) {
+ if (Klass.members.hasOwnProperty(prop)) {
+ that[prop] = Klass.members[prop];
+ }
+ }
+ }
+
+ delete Klass.init;
+ delete Klass.mixin;
+ delete Klass.members;
+
+ return that;
+ },
+
+ definitor = {
+
+ "inherit": function (Parent) {
+ Klass.parent = Parent;
+
+ return definitor;
+ },
+
+ "mixin": function () {
+ Klass.mixin = arguments;
+
+ return definitor;
+ },
+
+ "init": function (func) {
+ Klass.init = func;
+
+ return definitor;
+ },
+
+ "add": function (members) {
+ Klass.members = members;
+
+ return definitor;
+ },
+
+ "build": function () {
+ return Klass;
+ }
+
+ };
+
+ return definitor;
+
+ };
+
+ // Core
+ return {
+ "Class": Class
+ };
+
+ }());
+
+ exports.oops = exports.os = oops;
+
+}(window));

0 comments on commit 306b7e3

Please sign in to comment.