JavaScript面向对象之类的继承

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JavaScript中的继承</title>

<script type="text/javascript">
// 父类
function Person(name,email){   // 父类的构造方法
    this.name = name ;
    this.email = email ;
}

Person.prototype.getInfo = function(){
    return this.name+"的Email是:"+this.email ;
}

// 子类

function Employee(name,email,title){
    // 如同高级程序语言中的super()方法,调用父类的构造方法,但必须传入scope为子类对象的实例
    Person.call(this,name,email) ;
    // 子类特色的属性
    this.title = title ;
}
// 如果要继承Person,子类的prototype必须指定父类的实例

Employee.prototype = new Person () ;

var emp = new Employee('1212','11222@qq.com','title1234');

//  如果没有明确支持子类使用构造方法为父类的构造方法 
console.info(Employee.prototype);

console.info(emp.getInfo());

delete Employee.prototype.name ;   // 如果子类不需要父类的属性可以通过delete进行删除
//  如果子类和父类的构造方法处理事情不同可以明确的指出用子类的构造方法
Employee.prototype.constructor = Employee ;

console.info(Employee.prototype.constructor) ;
</script>


</head>

<body>
</body>
</html>
原文地址:https://www.cnblogs.com/xiaotao726/p/4713110.html