LOADING

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/cynchanpin/p/7308627.html
本文来源 爱码网,其版权均为 原网址 所有 与本站无关,文章内容系作者个人观点,不代表 本站 对观点赞同或支持。如需转载,请注明文章来源。

© 版权声明

相关文章