我是靠谱客的博主 甜蜜冥王星,这篇文章主要介绍使用apply方法实现javascript中的对象继承,现在分享给大家,希望可以做个参考。

复制代码 代码如下:

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

最后

以上就是甜蜜冥王星最近收集整理的关于使用apply方法实现javascript中的对象继承的全部内容,更多相关使用apply方法实现javascript中内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(232)

评论列表共有 0 条评论

立即
投稿
返回
顶部