码迷,mamicode.com
首页 > Web开发 > 详细

js 继承

时间:2016-04-16 12:13:55      阅读:178      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!