Skip to content

Instantly share code, notes, and snippets.

@maccman
Created February 11, 2011 13:56
Show Gist options
  • Select an option

  • Save maccman/822371 to your computer and use it in GitHub Desktop.

Select an option

Save maccman/822371 to your computer and use it in GitHub Desktop.
if (typeof Object.create !== "function")
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
};
var Model = {
prototype: {
init: function(){}
},
create: function(){
return Object.create(this);
},
inst: function(){
var instance = Object.create(this.prototype);
instance.init.apply(instance, arguments);
return instance;
}
};
jQuery.extend(Model, {
find: function(){},
});
jQuery.extend(Model.prototype, {
load: function(attributes){
for(var name in attributes)
this[name] = attributes[name];
}
});
var Asset = Model.create("Asset");
Asset.prototype.init = function(){
console.log('loaded')
};
var asset = Asset.inst();
@weepy
Copy link
Copy Markdown

weepy commented Feb 11, 2011

function inherits(child, parent) {
  var ctor = function(){};
  ctor.prototype = parent.prototype;
  child.prototype = new ctor();
  child.prototype.constructor = child;
  child.__super__ = parent.prototype;
}

var Model = function(klass, parent) {
  var cnstr = function() { 
    this.init.apply(this, arguments)
  }
  cnstr.prototype.init = function() {} 
  if(parent) {
     inherits(cnstr, parent)
     for(var i in parent) cnstr[i] = parent[i] 
  }

  return cnstr
}

var Asset = Model("Asset");
  
Asset.prototype.init = function(){
  console.log('loaded')
};
  
var asset = new Asset();

@maccman
Copy link
Copy Markdown
Author

maccman commented Feb 11, 2011

var SuperClass = function(parent){
  var klass = function(){
    this.init.apply(this, arguments);
  };

  // Change klass's prototype 
  if (parent) {
    var subclass = function() { };
    subclass.prototype = parent.prototype;
    klass.prototype = new subclass;
  };

  klass.prototype.init = function(){};

  // Shortcuts
  klass.fn = klass.prototype;
  klass.fn._class = klass;
  klass._super = klass.__proto__;      

  /* include/extend code... */

  return klass;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment