Javascript __Proto_uu;过时并使用getArea方法
我无法访问Triangle的get area函数,并得到一个不推荐的错误。Google(ed)将Crockford解决方案应用于Javascript __Proto_uu;过时并使用getArea方法,javascript,Javascript,我无法访问Triangle的get area函数,并得到一个不推荐的错误。Google(ed)将Crockford解决方案应用于\uuuuu Proto\uuuuuu,但将其应用于jsbin示例可能需要帮助 function extend(Child, Parent) { var F = function () {}; F.prototype = Parent.prototype; Child.prototype = new F();
\uuuuu Proto\uuuuuu
,但将其应用于jsbin示例可能需要帮助
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());
使用
Object.getPrototypeOf
代替()
您还可以使用它来命中构造函数,因此tri.\uuu proto\uuu.constructor
现在是Object.getPrototypeOf(tri).constructor
请注意,这与IE不向后兼容请改用
Object.getPrototypeOf
()
您还可以使用它来命中构造函数,因此tri.\uuu proto\uuu.constructor
现在是Object.getPrototypeOf(tri).constructor
请注意,这与IEF不向后兼容。请记住在问题中直接包含您的代码,以便将来的读者可以使用它。谢谢。请记住在问题中直接包含您的代码,以便将来的读者可以使用。谢谢
var prot = Object.getPrototypeOf(tri);
alert(prot.hasOwnProperty('name'));
if ( typeof Object.getPrototypeOf !== "function" ) {
if ( typeof "test".__proto__ === "object" ) {
Object.getPrototypeOf = function(object){
return object.__proto__;
};
} else {
Object.getPrototypeOf = function(object){
// May break if the constructor has been tampered with
return object.constructor.prototype;
};
}
}
function getProto(obj) {
return (typeof Object.getPrototypeOf !== 'undefined' ? Object.getPrototypeOf(obj) : obj.__proto__);
}