网络编程 
首页 > 网络编程 > 浏览文章

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

(编辑:jimmy 日期: 2025/7/4 浏览:3 次 )
复制代码 代码如下:
<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修改css样式style动态改变元素样式
下一篇:javaScript如何生成xmlhttp
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 网站地图 SiteMap