会员
商店
众包
新闻
博问
闪存
赞助商
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
博客园
首页
新随笔
联系
订阅
管理
Js对象继承
1.对象冒充
<!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=utf-8"> <title>Document</title> </head> <body> <script type="text/javascript"> function Person(personName){ this.name = personName; this.sayName = function(){ alert('my name is ' + this.name); } } function Man(name, age){ //Person.call(this, name); Person.apply(this,new Array(name)); this.age = age; this.sayAge = function(){ alert('my age is ' + this.age); } } var m = new Man('jikey', 'tom', 29); m.sayName(); m.sayAge(); </script> </body> </html>
运行代码
2.混合方式
<!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=utf-8" /> <title>Document</title> <style> </style> <script> function Person(name){ this.name = name; } Person.prototype.sayName = function(){ alert('My name is ' + this.name);} function Man(name, age){ Person.call(this, name); this.age = age; } Man.prototype = new Person(); Man.prototype.sayAge = function(){ alert('My age is ' + this.age);} var tom = new Man('tom', 29); var jikey = new Man('jikey', 29); tom.sayAge(); tom.sayName(); jikey.sayName(); </script></head> <body> </body> </html>
运行代码
posted @
2010-05-05 14:25
豪情
阅读(
853
) 评论(
0
)
编辑
收藏
举报
努力加载评论中...
刷新页面
返回顶部
【推荐】100%开源!大型工业跨平台软件C++源码提供,建模,组态!
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
公告
点击右上角即可分享
AI IDE Trae
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步