Advertisement
sashomaga

Prototypal OOP

Jun 1st, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. // Extend
  4. if (!Object.create) {
  5.     Object.create = function (obj) {
  6.         function f() { };
  7.         f.prototype = obj;
  8.         return new f();
  9.     }
  10. }
  11.  
  12. if (!Object.prototype.extend) {
  13.     Object.prototype.extend = function (properties) {
  14.         function f() { };
  15.         f.prototype = Object.create(this);
  16.         for (var prop in properties) {
  17.             f.prototype[prop] = properties[prop];
  18.         }
  19.         f.prototype._super = this;
  20.         return new f();
  21.     }
  22. }
  23.  
  24. // Prototypal OOP
  25. var Human = {
  26.     init: function (fname, lname, age) {
  27.         this.fname = fname;
  28.         this.lname = lname;
  29.         this.age = age;
  30.     }
  31. };
  32.  
  33. var Student = Human.extend({
  34.     init: function (fname, lname, age, grade) {
  35.         this._super.init(fname, lname, age);
  36.         this.grade = grade;
  37.     },
  38.     introduce: function () {
  39.         return "Hello! My name is " + this.fname + " " + this.lname + ", my age is: " + this.age + " grade: " + this.grade;
  40.     }
  41. });
  42.  
  43. var pesho = Object.create(Student);
  44. pesho.init("Pesho", "Mastikata", 12, 2);
  45. console.log(pesho.introduce());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement