From fdffc4fa32dc5964da6689209ba8741e1ef18f08 Mon Sep 17 00:00:00 2001 From: pazguille Date: Thu, 8 Dec 2011 08:57:54 -0300 Subject: [PATCH] Added Object.create in extend method. --- index.html | 15 ++++---- oop.js | 108 ++++++++++++++++++++++++++++------------------------- 2 files changed, 65 insertions(+), 58 deletions(-) diff --git a/index.html b/index.html index b2c0874..0e52295 100644 --- a/index.html +++ b/index.html @@ -11,36 +11,37 @@ this.name = name; }) .method("sleep", function () { - alert("ZzZzZzZzZzZzZzZzZz"); + console.log("ZzZzZzZzZzZzZzZzZz"); }) .build(); var Cat = oop.Class("Cat") .extends(Animal) .method("purr", function () { - alert("purrrrrrrrrrrrr"); + console.log("purrrrrrrrrrrrr"); }) .build(); - + var Dog = oop.Class("Dog") .extends(Animal) .method("bark", function () { - alert("Wua bark woop arf"); + console.log("Wua bark woop arf"); }) - .build(); + .build(); var lilo = new Cat("Lilo"); var luz = new Dog("Luz"); - alert(lilo.name); // Lilo - alert(luz.name); // Luz + console.log(lilo.name); // Lilo + console.log(luz.name); // Luz lilo.sleep(); // ZzZzZzZzZzZzZzZzZz luz.sleep(); // ZzZzZzZzZzZzZzZzZz lilo.purr(); // purrrrrrrrrrrrr luz.bark(); // Wua bark woop arf + \ No newline at end of file diff --git a/oop.js b/oop.js index 848526b..6f19acc 100644 --- a/oop.js +++ b/oop.js @@ -1,68 +1,74 @@ -(function(window, undefined) { - var oop = (function(){ +(function(window, undefined) { - var Class = function (klass) { - var Klass, definitor; + Object.create = Object.create || (function(){ + var F = function(){}; + return function (obj) { + F.prototype = obj; + return new F(); + }; + }()); - Klass = function (conf) { - var that = this; - var conf = conf || {}; - this.type = klass; + var oop = (function(){ - if (Klass.parent && Klass.parent.hasOwnProperty("construct")) { Klass.parent.construct.call(this, conf); - } - - if (Klass.prototype.hasOwnProperty("construct")) { - Klass.prototype.construct.call(this, conf); } + var Class = function (klass) { + var Klass, definitor; + + Klass = function (conf) { + // Forget new prefix? + var that = (this instanceof Klass) ? this : Object.create(Klass.prototype); - return this; - }; + var conf = conf || {}; + that.type = klass; - definitor = { + if (Klass.parent && Klass.parent.hasOwnProperty("construct")) { Klass.parent.construct.call(that, conf); + } + + if (Klass.prototype.hasOwnProperty("construct")) { + Klass.prototype.construct.call(that, conf); } - "extends": (function () { - var F = function () {}; - return function (Parent) { - F.prototype = Parent.prototype; - Klass.prototype = new F(); + return that; + }; + + definitor = { + + "extends": function (Parent) { + Klass.prototype = Object.create(Parent.prototype); Klass.parent = Parent.prototype; - Klass.prototype.constructor = Klass; - + return definitor; - } - }()), - - "construct": function (func) { - Klass.prototype.construct = func; - - return definitor; - }, - - "method": function (member, param) { - Klass.prototype[member] = param; + }, + + "construct": function (func) { + Klass.prototype.construct = func; + + return definitor; + }, + + "method": function (member, param) { + Klass.prototype[member] = param; + + return definitor; + }, - return definitor; - }, - - "build": function () { - return Klass; - } + "build": function () { + return Klass; + } + + }; + + return definitor; + + }; + var core = { + "Class": Class }; - return definitor; - - }; - - var core = { - "Class": Class - }; - - return core; + return core; - })(); + })(); - window.oop = oop; + window.oop = oop; })(window); \ No newline at end of file