js完美继承代码示例

时间:2023-03-09 15:40:41
js完美继承代码示例
 <!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
<!-- 定义继承函数-->
var inherits = function(subType,superType){
var F = function(){};
F.prototype = superType.prototype;
subType.prototype = new F();
subType.prototype.constructor = subType;
}
//定义父元素
function a(){
this.q = 2;
};
a.prototype.cc="ert";
//定义父元素;
function b(){
//a、 子元素内部调用call继承父元素的this的属性、方法
a.call(this)
};
//b、 运行继承函数继承prototype的属性、方法;
inherits(b,a);
var c = new b();
console.log(c.q)
</script>
</body>
</html>