使用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>
相关文章
原生js XMLhttprequest请求onreadystatechange执行两次的解决
这篇文章主要介绍了原生js XMLhttprequest请求onreadystatechange执行两次的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-02-02JS信息收集代码研究(获取客户端的浏览器、cpu等软硬件信息)
听朋友说,这段JavaScript信息收集代码主要用于收集IP......等等什么的,用来研究,需要的朋友可以参考一下2023-06-06
最新评论