加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 教程 > 正文

运用apply方法实现javascript中的对象继承

发布时间:2023-08-26 14:38:44 所属栏目:教程 来源:
导读:代码如下:

<script type="text/javascript">

//使用apply方法实现对象继承

function Parent(username) {

this.username = username;

this.sayHello = function() {

alert(this.username);
代码如下:
 
<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));
 
//和下面一样
 
//Parent.apply(this, [username]);
 
this.password = password;
 
this.sayWorld = function() {
 
alert(this.password);
 
}
 
}
 
var parent = new Parent("zhangsan");
 
var child = new Child("lisi", "123");
 
parent.sayHello();
 
child.sayHello();
 
child.sayWorld();
 
</script>
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章