使用apply方法实现javascript中的对象继承
时间:2021-09-19 07:53:00|栏目: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>