sashomaga

Create class pattern

Jun 30th, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Class = (function() {
  2.   function subclass() {}
  3.  
  4.   function createClass(properties) {
  5.     function f() {
  6.       this.init.apply(this, arguments);
  7.     }
  8.  
  9.     var prototype = {};
  10.     for (var prop in properties) {
  11.       prototype[prop] = properties[prop];
  12.     }
  13.     if (!prototype.init) {
  14.       prototype.init = function() {};
  15.     }
  16.  
  17.     f.prototype = prototype;
  18.     f.extend = extendClass;
  19.  
  20.     return f;
  21.   }
  22.  
  23.   var initializing = false;
  24.  
  25.   function extendClass(properties) {
  26.     var _super = this.prototype;
  27.  
  28.     initializing = true;
  29.     var prototype = new this();
  30.     initializing = false;
  31.  
  32.     for (var name in properties) {
  33.       var value = properties[name];
  34.       if (_super[name] && typeof value === "function" && typeof _super[name] === "function" && value.toString().indexOf("this._super(") >= 0) {
  35.         value = (function(name, fn) {
  36.           return function() {
  37.             var tmp = this._super;
  38.             this._super = _super[name];
  39.             var ret = fn.apply(this, arguments);
  40.             this._super = tmp;
  41.             return ret;
  42.           };
  43.         }(name, value));
  44.       }
  45.       prototype[name] = value;
  46.     }
  47.  
  48.     function f() {
  49.       if (!initializing && this.init) {
  50.         this.init.apply(this, arguments);
  51.       }
  52.     }
  53.  
  54.     f.prototype = prototype;
  55.  
  56.     f.prototype.constructor = f;
  57.     f.extend = extendClass;
  58.     return f;
  59.   }
  60.  
  61.   return {
  62.     create: createClass
  63.   };
  64. }());
  65.  
  66. var Person = Class.create({
  67.             init: function(name, age){
  68.                 this.name = name;
  69.                 this.age = age;
  70.             },
  71.             say: function(){
  72.                 console.log("person say");
  73.             }
  74.         });
  75.        
  76. var Student = Person.extend({
  77.             init: function(name, age, grade){
  78.                 console.log(this._super);
  79.                 this._super(name, age);
  80.                 this.grade = grade;
  81.                 // some way to save bace function
  82.                 if(this._super){
  83.                     var parent = new Person();
  84.                     this.base_say = parent.say;
  85.                 }
  86.             },
  87.             say: function(){
  88.                 //if there is override
  89.                 if(this._super){
  90.                     return this._super();
  91.                 } else{
  92.                     console.log("student say")
  93.                 }
  94.             }
  95.         });
  96.        
  97. var pesho = new Student("Pesho", "12", "A");
  98. pesho.say();
Advertisement
Add Comment
Please, Sign In to add comment