• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

cynchanpin

  • 博客园
  • 联系
  • 订阅
  • 管理

View Post

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>

posted on 2017-08-08 19:41  cynchanpin  阅读(167)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3