Skip to content

Instantly share code, notes, and snippets.

@ebertech
Forked from phillmv/inheritance.js
Created March 28, 2012 03:35
Show Gist options
  • Save ebertech/2223341 to your computer and use it in GitHub Desktop.
Save ebertech/2223341 to your computer and use it in GitHub Desktop.
function inherit(p) {
if (p == null) throw TypeError();
if (Object.create)
return Object.create(p);
var t = typeof p;
if (t !== "object" && t !== "function") throw TypeError();
function f() {};
f.prototype = p;
return new f();
}
function FormObject(form_id)
{
this.form_id = form_id;
this.attr_name = this.attr_base() + "[name]";
this.attr_form_name = this.attr_base() + "[form_identifier]";
this.to_html = function() {
return "<td>" +
"<input name='" + this.attr_name + "' type='text'>" +
"<input name='" + this.attr_form_name + "' type='hidden' value='" + this.form_id + "'>'" +
"</td>";
}
return this;
};
FormObject.prototype.attr_base = function() { return "" }
function OptionCategory(form_id) {
FormObject.apply(this, arguments);
}
OptionCategory.prototype = inherit(FormObject.prototype);
OptionCategory.prototype.constructor = OptionCategory;
OptionCategory.prototype.attr_base = function(){ return "fun"}
/*
I want to create a OptionCategory object that "inherits" the above and overrides this.attr_base
*/
var me = new FormObject("wibble");
console.log(me.to_html());
var you = new OptionCategory("blort");
console.log(you.to_html());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment