http://jsbin.com/ifoguf/19/edit
I’m unable to access the get area function of Triangle, and getting a deprecated error. Google(ed) the Crockford solution to __Proto__, but could use help applying it to the jsbin example.
function extend(Child, Parent) {
var F = function () {};
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
Child.uber = Parent.prototype;
}
function extend2(Child, Parent) {
var p = Parent.prototype;
var c = Child.prototype;
for (var i in p) {
c[i] = p[i];
}
c.uber = p;
}
var Shape = function () {};
var TwoDShape = function () {};
var Triangle = function (side, height) {
this.side = side;
this.height = height;
};
// agument prototype of Shape;
Shape.prototype.name = 'shape';
Shape.prototype.toString = function () {
return this.name;
};
// augment prototype of Triangle;
Triangle.prototype.name = 'Triangle';
Triangle.prototype.getArea = function () {
return this.side * this.height / 2;
};
extend2(TwoDShape, Shape);
extend2(Triangle, TwoDShape);
var td = new TwoDShape();
var tri = new Triangle(5, 10);
alert(tri.__proto__.hasOwnProperty('name'));
alert(td.name);
alert(tri.constructor.getArea());
Use
Object.getPrototypeOfinstead (docs)You would also use that to hit the constructor, so
tri.__proto__.constructoris nowObject.getPrototypeOf(tri).constructorNote that this is not backward compatible with IE<9, so you will still have to use the deprecated method for those browsers. John Resig suggested this as a potential “good enough for now” function to address the lack of user agent support for
getPrototypeOf:Reference: http://ejohn.org/blog/objectgetprototypeof/
I find this to be functional, but perhaps a bit over-blown – this is what I use: