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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2020-05-13 我要评论
复制代码 代码如下:

<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>

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们