使用apply方法实现javascript中的对象继承
更新时间:2013年12月16日 17:18:14 作者:
javascript中的对象继承的方法有很多,在接下来的文章中为大家介绍下使用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));
//和下面一样
//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>
相关文章
javascript设计模式 – 单例模式原理与应用实例分析
这篇文章主要介绍了javascript设计模式 – 单例模式原理与应用,结合实例形式分析了javascript单例模式原理、定义、应用场景及相关操作注意事项,需要的朋友可以参考下2020-04-04


最新评论