首页 > Web开发 > 详细

js 继承

时间:2016-04-16 12:11:16      阅读:230      评论:0      收藏:0      [点我收藏+]
<html>
<head>
<script>
function extend(Child,Parent){
    var F = function(){};
    F.prototype = Parent.prototype;
    Child.prototype=new F();
    Child.prototype.constructor=Child;
    Child.uber=Parent.prototype;
}

function Shape() {}
Shape.prototype.name = ‘Shape‘;
Shape.prototype.toString = function () {
    return this.constructor.uber
    ? this.constructor.uber.toString() + ‘, ‘ + this.name
    : this.name;
};


function TwoDShape() {}
extend(TwoDShape,Shape);
TwoDShape.prototype.name = ‘2D shape‘;


function Triangle(side, height) {
this.side = side;
this.height = height;
}
extend(Triangle,TwoDShape);
Triangle.prototype.name = ‘Triangle‘;
Triangle.prototype.getArea = function () {
    return this.side * this.height / 2;
};


function myclick(){
    var my = new Triangle();
    console.log(my.toString());
}
</script>
</head>
<body>
<div style="width:100px;height:100px;background:red;" onclick="myclick()"></div>
</body>
</html>

 

js 继承

原文:http://www.cnblogs.com/zhangwei595806165/p/5397869.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!