六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 25|回复: 0

javascript学习五

[复制链接]

升级  76%

10

主题

10

主题

10

主题

童生

Rank: 1

积分
38
 楼主| 发表于 2013-1-29 08:55:14 | 显示全部楼层 |阅读模式
<script type="text/javascript">//使用call实现继承function parent(username){this.username=username;this.sayHello=function(){alert(this.username);}}function child(username,password){parent.call(this,username);this.password=password;this.sayWord=function(){alert(this.password);}}var p=new parent("hello");var c=new child("word","123");p.sayHello();c.sayHello();c.sayWord();</script> apply方法方式:
<script type="text/javascript">//apply方法实现对象继承function parent(username){this.username=username;this.sayHello=function(){alert(this.username);}}function child(username,password){parent.apply(this,new Array(username));this.password=password;this.sayWord=function(){alert(this.password);}}var p=new parent("hello");var c=new child("word","123");p.sayHello();c.sayHello();c.sayWord();</script> 原型链方式:无法给构造函数传递参数
<script type="text/javascript">//使用原型链(prototype chain)实现对象的继承function parent(){}parent.prototype.hello="hello";parent.prototype.sayHello=function(){alert(this.hello);}function child(){}child.prototype=new parent();child.prototype.word="word";child.prototype.sayWord=function(){alert(this.word);}var p=new parent();var c=new child();p.sayHello();c.sayHello();c.sayWord();</script> 混合方式:(推荐使用)
<script type="text/javascript">//使用混合方式实现对象的继承function parent(hello){this.hello=hello;}parent.prototype.sayHello=function(){alert(this.hello);}function child(hello,word){//实现属性的继承parent.call(this,hello);this.word=word;}//实现方法的继承child.prototype=new parent();child.prototype.sayWord=function(){alert(this.word);}var p=new parent("hello");var c=new child("word","123");p.sayHello();c.sayHello();c.sayWord();</script> 学到这里,javascript的核心应该已经学完了,可是自己感觉没什么进步,不知道大家是怎么在学习啊,希望大家能指教指教小弟。
 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表