Skip to content

Commit

Permalink
Added Object.create in extend method.
Browse files Browse the repository at this point in the history
  • Loading branch information
pazguille committed Dec 8, 2011
1 parent 18fae79 commit fdffc4f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 58 deletions.
15 changes: 8 additions & 7 deletions index.html
Expand Up @@ -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

</script>
</body>
</html>
108 changes: 57 additions & 51 deletions 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);
Expand Down

0 comments on commit fdffc4f

Please sign in to comment.