JavaScript的继承方法

来源:互联网 发布:大数据魔镜官网 编辑:程序博客网 时间:2024/04/28 13:41

JavaScript是面向对象的语言,所以我们可以说,JS是可以继承的,但是它是一种 动态语言,所以他的继承肯定和

其他的语言不同,下面我们来说一下JS的对象继承机制。

先看一下代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>test1.html</title>    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="this is my page">    <meta http-equiv="content-type" content="text/html; charset=UTF-8">    <link rel="stylesheet" href="../css/5.css" type="text/css"></link>   <script type="text/javascript"> function Stu(name,age){ this.name = name; this.age = age; this.show=function(){ window.alert(name+"  "+age); } }   function MidStu(name,age){ this.stu = Stu; this.stu(name,age);//JS实际上是通过对象冒充的方式实现继承,这句话很重要 }  var temp = new MidStu("a",123); temp.show(); </script>  </head>    <body>    </body></html>

可以看出,JS是通过对象冒充的方式实现继承的,而且不难想想,他也是可以实现多重继承的效果。